Jump to page: 1 2
Thread overview
How to use the -I command line switch?
Jan 03
tipdbmp
Jan 03
jmh530
Jan 03
tipdbmp
Jan 03
jmh530
Jan 03
Tony
Jan 03
jmh530
Jan 03
Seb
Jan 04
jmh530
Jan 03
Tony
January 03
// C:\libs\my_module.d
module my_module;
void foo() {}

// main.d
module main;
import my_module;

void main() {
    foo();
}

Running dmd with:
    dmd -IC:\libs main.d my_module.d

I get:
    Error: module my_module is in file 'my_module.d' which cannot be read
    import path[0] = C:\libs
    import path[1] = path\to\dmd\D\dmd2\windows\bin\..\..\src\phobos
    import path[2] = path\to\dmd\D\dmd2\windows\bin\..\..\src\druntime\import

January 03
On Wednesday, 3 January 2018 at 12:21:28 UTC, tipdbmp wrote:
> // C:\libs\my_module.d
> module my_module;
> void foo() {}
>
> // main.d
> module main;
> import my_module;
>
> void main() {
>     foo();
> }
>
> Running dmd with:
>     dmd -IC:\libs main.d my_module.d
>
> I get:
>     Error: module my_module is in file 'my_module.d' which cannot be read
>     import path[0] = C:\libs
>     import path[1] = path\to\dmd\D\dmd2\windows\bin\..\..\src\phobos
>     import path[2] = path\to\dmd\D\dmd2\windows\bin\..\..\src\druntime\import


dmd main.d C:\libs\my_module.d

I usually use dub for this type of thing...
January 03
> dmd main.d C:\libs\my_module.d
That does not use the -I switch.

It compiles if I specify the full path to my_module.d:
    dmd -IC:\libs main.d C:\libs\my_module.d

I don't understand the error message though.

January 03
On Wednesday, 3 January 2018 at 17:10:22 UTC, tipdbmp wrote:
>> dmd main.d C:\libs\my_module.d
> That does not use the -I switch.
>
> It compiles if I specify the full path to my_module.d:
>     dmd -IC:\libs main.d C:\libs\my_module.d
>
> I don't understand the error message though.

I tried a few other options, but I don't use it enough to know the trick to get it working. I tend to use either dub, setting the path environmental variables, or the absolute path. Hopefully someone else can let you know. If it's a bug, it should be in bugzilla, but sometimes I just assume I screwed something up.
January 03
On 01/03/2018 09:10 AM, tipdbmp wrote:
>> dmd main.d C:\libs\my_module.d
> That does not use the -I switch.
> 
> It compiles if I specify the full path to my_module.d:
>      dmd -IC:\libs main.d C:\libs\my_module.d
> 
> I don't understand the error message though.
> 

-I is for import directives only. imports are needed to compile the importing module. All other modules still need to be compiled themselves and added to the program either as individual .o files or as libraries (e.g. .a, .lib, etc.).

The method you've shown is a shorthand for "compile each to .o and add each to the program."

Working as expected... :)

Ali
January 03
On Wednesday, 3 January 2018 at 18:35:21 UTC, Ali Çehreli wrote:
> On 01/03/2018 09:10 AM, tipdbmp wrote:
>>> dmd main.d C:\libs\my_module.d
>> That does not use the -I switch.
>> 
>> It compiles if I specify the full path to my_module.d:
>>      dmd -IC:\libs main.d C:\libs\my_module.d
>> 
>> I don't understand the error message though.
>> 
>
> -I is for import directives only. imports are needed to compile the importing module. All other modules still need to be compiled themselves and added to the program either as individual .o files or as libraries (e.g. .a, .lib, etc.).
>
> The method you've shown is a shorthand for "compile each to .o and add each to the program."
>
> Working as expected... :)
>
What about the error message? If -I is only for DMD finding "import ..." files, and not files on the command line, why does DMD list what was in the -I "where to look for import directives" when saying that it can't find a command-line file? It says that it can't locate my_module.d and then lists the directory that my_module.d is in.

January 03
On Wednesday, 3 January 2018 at 18:35:21 UTC, Ali Çehreli wrote:
>
> -I is for import directives only. imports are needed to compile the importing module. All other modules still need to be compiled themselves and added to the program either as individual .o files or as libraries (e.g. .a, .lib, etc.).
>
> The method you've shown is a shorthand for "compile each to .o and add each to the program."
>
> Working as expected... :)
>
> Ali

Is there any way to re-write the documentation so its clearer? I've been confused by this before as well...
January 03

On 01/03/2018 01:42 PM, Tony wrote:
> On Wednesday, 3 January 2018 at 18:35:21 UTC, Ali Çehreli wrote:

>> Working as expected... :)
>>
> What about the error message? If -I is only for DMD finding "import ..."
> files, and not files on the command line, why does DMD list what was in
> the -I "where to look for import directives" when saying that it can't
> find a command-line file? It says that it can't locate my_module.d and
> then lists the directory that my_module.d is in.

Yeah, there's definitely a problem there.

It's possible that the compiler is acting like this: Because there is already my_module.d on the command line, it assumes that main's 'import my_module' must mean that file. Because there is no such file in the local directory, it errors out by using a generic error message function, which does not explain the situation well.

I made that all up but I bet it's something like that. :)

Ali
January 03
On Wednesday, 3 January 2018 at 12:21:28 UTC, tipdbmp wrote:
> // C:\libs\my_module.d
> module my_module;
> void foo() {}
>
> // main.d
> module main;
> import my_module;
>
> void main() {
>     foo();
> }
>
> Running dmd with:
>     dmd -IC:\libs main.d my_module.d
>
> I get:
>     Error: module my_module is in file 'my_module.d' which cannot be read
>     import path[0] = C:\libs
>     import path[1] = path\to\dmd\D\dmd2\windows\bin\..\..\src\phobos
>     import path[2] = path\to\dmd\D\dmd2\windows\bin\..\..\src\druntime\import

As has already been mentioned, the -I is not used for command-line files. Just compiling (-c option) shows that the -I is enough for DMD to find the import file:

dmd -c main.d -Ic:\libs

successfully compiles main.d into main.obj

To do a full compile and link of main without compiling my_module.d each time:

C:\libs>dmd -lib -ofmy_module.lib my_module.d

creates "my_module.lib". Then use it to link with in main.d compile/link:

C:\code\d\forum>dmd main.d -Ic:\libs -Llib c:\libs\my_module
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
OPTLINK : Warning 9: Unknown Option : NOILIB


main.exe is created even though there is a mysterious warning.
January 03
On Wednesday, 3 January 2018 at 21:51:07 UTC, jmh530 wrote:
> On Wednesday, 3 January 2018 at 18:35:21 UTC, Ali Çehreli wrote:
>>
>> -I is for import directives only. imports are needed to compile the importing module. All other modules still need to be compiled themselves and added to the program either as individual .o files or as libraries (e.g. .a, .lib, etc.).
>>
>> The method you've shown is a shorthand for "compile each to .o and add each to the program."
>>
>> Working as expected... :)
>>
>> Ali
>
> Is there any way to re-write the documentation so its clearer? I've been confused by this before as well...

Which documentation are you referring to? If it's the specification, just click "edit".
« First   ‹ Prev
1 2