digitalmars.D.learn - Including a file
- Vindex (14/14) Jul 18 2021 One of my library modules (for example, libmylib) has the line
- Adam D Ruppe (3/5) Jul 18 2021 You need to specify the path where it is found with the -J switch
- Vindex (4/9) Jul 18 2021 I understand it. But it turns out that in order to compile any
- Vindex (4/9) Jul 18 2021 I already compiled the library itself with -Jres. It turns out
- Mathias LANG (52/62) Jul 18 2021 Is the inclusion done in a templated function, or in a global ?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (11/17) Jul 18 2021 step?
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
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 -JYou need to specify the path where it is found with the -J switch to the compiler. Like `ldc2 -J. yourfile.d
Jul 18 2021
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: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?Error: file "thing.json" cannot be found or not in a path specified with -JYou need to specify the path where it is found with the -J switch to the compiler. Like `ldc2 -J. yourfile.d
Jul 18 2021
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: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.Error: file "thing.json" cannot be found or not in a path specified with -JYou need to specify the path where it is found with the -J switch to the compiler. Like `ldc2 -J. yourfile.d
Jul 18 2021
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: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.On Sunday, 18 July 2021 at 17:28:07 UTC, Vindex wrote: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.Error: file "thing.json" cannot be found or not in a path specified with -JYou need to specify the path where it is found with the -J switch to the compiler. Like `ldc2 -J. yourfile.d
Jul 18 2021
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 specifiedwith -J``` Is the actual inclusion of the file on import deferred until the linkstep? 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 dothat. 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