Including a file

Mathias LANG geod24 at gmail.com
Mon Jul 19 01:33:01 UTC 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:
>> 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.


More information about the Digitalmars-d-learn mailing list