www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Scope of import

reply DLearner <bmqazwsx123 gmail.com> writes:
```
// main
void main() {
    import A;
// import B;
    import std.stdio;

    writeln("Entered main");

    fnA1();

    writeln("Leaving main");
}
```

```
module A;

void fnA1() {

    import B;
    import std.stdio;

    writeln("Entered fnA1");
    fnB1();
    writeln("Leaving fnA1");
}
```

```
module B;

void fnB1() {

    import std.stdio;

    writeln("Entered fnB1");
    writeln("Leaving fnB1");
}
```

1. Code above compiles but fails on linker step with 'Error 42 
Symbol Undefined'.
To me, unexpected behaviour as imports arranged to pick up 
symbols (with minimum scope).

2. Uncommenting the 'import B' in main everything works correctly.
To me, particularly unexpected behaviour as no symbol from B 
directly used in main (also undesirable to set scope 
unnecessarily wide).

Best regards
May 14 2021
parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 15 May 2021 at 06:55:47 UTC, DLearner wrote:

 1. Code above compiles but fails on linker step with 'Error 42 
 Symbol Undefined'.
 To me, unexpected behaviour as imports arranged to pick up 
 symbols (with minimum scope).
Your error is a linker error. Imports have nothing to do with the linker. They are for the compiler to know which symbols are available in the module it's currently compiling. But every symbol that you use needs to be linked by the linker, and that's a separate step.
 2. Uncommenting the 'import B' in main everything works 
 correctly.
That's odd. What's your command line?
 To me, particularly unexpected behaviour as no symbol from B 
 directly used in main (also undesirable to set scope 
 unnecessarily wide).
Whether you use a symbol in main or not is irrelevant to the linker. Any symbol accessed anywhere in your program must ultimately be linked in.
May 15 2021
parent reply DLearner <bmqazwsx123 gmail.com> writes:
On Saturday, 15 May 2021 at 07:05:00 UTC, Mike Parker wrote:
 That's odd. What's your command line?
rdmd main.d
May 15 2021
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 15 May 2021 at 07:15:51 UTC, DLearner wrote:
 On Saturday, 15 May 2021 at 07:05:00 UTC, Mike Parker wrote:
 That's odd. What's your command line?
rdmd main.d
Then it must be an issue with rdmd. The following both work as expected, whether your the `import B;` in `main` is commented out or not: dmd -i main.d dmd main.d A.d B.d
May 15 2021
parent DLearner <bmqazwsx123 gmail.com> writes:
On Saturday, 15 May 2021 at 07:19:08 UTC, Mike Parker wrote:
 Then it must be an issue with rdmd...
rdmd build 20210311 Running under Win-10.
May 15 2021
prev sibling next sibling parent Mike Parker <aldacron gmail.com> writes:
On Saturday, 15 May 2021 at 07:15:51 UTC, DLearner wrote:
 On Saturday, 15 May 2021 at 07:05:00 UTC, Mike Parker wrote:
 That's odd. What's your command line?
rdmd main.d
Okay, so it's definitely a bug in rdmd. Change module A to look like this and it works properly: ```d module A; import B; void fnA1() { import std.stdio; writeln("Entered fnA1"); fnB1(); writeln("Leaving fnA1"); } ```
May 15 2021
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 15 May 2021 at 07:15:51 UTC, DLearner wrote:
 rdmd main.d
rdmd sucks, it runs the compiler twice and get the list of imports and even then it might not see them all. Just use dmd -i main.d instead. It will be about 2x faster and more reliable. The downside differences though: * rdmd runs the program too, dmd -i just compiles. You run the program separately. * rdmd will cache the executable, dmd -i will always recompile. But since running the program is a separate step, you just run it yourself if you don't want to recompile, and run dmd if you do.
May 15 2021
next sibling parent reply Dennis <dkorpel gmail.com> writes:
On Saturday, 15 May 2021 at 11:38:22 UTC, Adam D. Ruppe wrote:
 * rdmd runs the program too, dmd -i just compiles. You run the 
 program separately.
You can do `dmd -i -run main.d`
May 15 2021
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 15 May 2021 at 11:46:49 UTC, Dennis wrote:
 You can do `dmd -i -run main.d`
Yeah but that's weird with how it handles arguments and without the compilation cache it gets really annoying to use.
May 15 2021
parent Alain De Vos <devosalain ymail.com> writes:
In unix i give the compiler:
```
ldc2 `find . -name \*.d -print`
```
So he always takes all sourcefiles
May 15 2021
prev sibling parent DLearner <bmqazwsx123 gmail.com> writes:
On Saturday, 15 May 2021 at 11:38:22 UTC, Adam D. Ruppe wrote> 
Just use
```
 dmd -i main.d

 instead. It will be about 2x faster and more reliable.
``` Your suggestion worked. Thank you.
May 15 2021