www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Including a file

reply Vindex <tech.vindex gmail.com> writes:
One of my library modules (for example, libmylib) has the line

`immutable string jsonText = import("thing.json")`


When I try to build a program with this (already installed) 
library I see error

```
ldc2 source/main.d -ofmyprogram -L-l:libmylib.so.0
Error: file "thing.json" cannot be found or not in a path 
specified with -J
```

Is the actual inclusion of the file on import deferred until the 
link step?
Is there a way to avoid this?
I could insert the entire json into the file, but I would hate to 
do that.
Jul 18 2021
next sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Sunday, 18 July 2021 at 17:28:07 UTC, Vindex wrote:
 Error: file "thing.json" cannot be found or not in a path 
 specified with -J
You need to specify the path where it is found with the -J switch to the compiler. Like `ldc2 -J. yourfile.d
Jul 18 2021
next sibling parent Vindex <tech.vindex gmail.com> writes:
On Sunday, 18 July 2021 at 17:31:24 UTC, Adam D Ruppe wrote:
 On Sunday, 18 July 2021 at 17:28:07 UTC, Vindex wrote:
 Error: file "thing.json" cannot be found or not in a path 
 specified with -J
You need to specify the path where it is found with the -J switch to the compiler. Like `ldc2 -J. yourfile.d
I understand it. But it turns out that in order to compile any programs with such a library, you need to specify the path to the resource directory of this library?
Jul 18 2021
prev sibling parent reply Vindex <tech.vindex gmail.com> writes:
On Sunday, 18 July 2021 at 17:31:24 UTC, Adam D Ruppe wrote:
 On Sunday, 18 July 2021 at 17:28:07 UTC, Vindex wrote:
 Error: file "thing.json" cannot be found or not in a path 
 specified with -J
You need to specify the path where it is found with the -J switch to the compiler. Like `ldc2 -J. yourfile.d
I already compiled the library itself with -Jres. It turns out that this must be done twice? Once when compiling the library, the second time when compiling the program.
Jul 18 2021
parent Mathias LANG <geod24 gmail.com> writes:
On Sunday, 18 July 2021 at 17:48:53 UTC, Vindex wrote:
 On Sunday, 18 July 2021 at 17:31:24 UTC, Adam D Ruppe wrote:
 On Sunday, 18 July 2021 at 17:28:07 UTC, Vindex wrote:
 Error: file "thing.json" cannot be found or not in a path 
 specified with -J
You need to specify the path where it is found with the -J switch to the compiler. Like `ldc2 -J. yourfile.d
I already compiled the library itself with -Jres. It turns out that this must be done twice? Once when compiling the library, the second time when compiling the program.
Is the inclusion done in a templated function, or in a global ? If the `import` happens in a function that is not in a root module, it won't be necessary to use `-J` when linking the program: ```D module root; import nonroot; import std.stdio; void main () { writeln(foo()); } // Some other file: module nonroot; string foo () { string val = import("hello.txt"); return foo; } ``` However, if the `import` is in a templated function, it might need to be instantiated in the calling module: ``` module root; import nonroot; import std.stdio; void main () { writeln(foo!"hello.txt"()); } // Some other file: module nonroot; string foo (string path) () { string val = import(path); return foo; } ``` This will error out because the `foo` function needs to be generated inside of the `root` module context: ``` nonroot.d(5): Error: need `-J` switch to import text file `hello.txt` nonroot.d(6): Error: template `foo(string path)()` has no type root.d(6): Error: template instance `nonroot.foo!"hello.txt"` error instantiating ``` An easy way for you to solve this might be to hide the value of your JSON file behind a simple function that returns it, so that the compiler never analyzes its body. If you use LDC and LTO, I doubt it'll make any difference.
Jul 18 2021
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 7/18/21 10:28 AM, Vindex wrote:

 ```
 ldc2 source/main.d -ofmyprogram -L-l:libmylib.so.0
 Error: file "thing.json" cannot be found or not in a path specified 
with -J
 ```

 Is the actual inclusion of the file on import deferred until the link 
step? I wonder whether it's an ldc thing related to its link-time powers?
 I could insert the entire json into the file, but I would hate to do 
that. I insert two many-megabytes long tar.gz files in there. :) I unzip and untar the files into a cache directory to use at runtime. I just works. The zipped version of a json file may be very small. I my case, the files are gzip'ped during the build stage and I unzip with https://dlang.org/phobos/std_zlib.html Ali
Jul 18 2021