www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Importing a module from another directory

reply "Derix" <derix dexample.com> writes:
I try to compile the basic HelloWorld.d example in which I only
added

    import Journal;

where Journal is a module I defined in Journal.d in another
directory.

So I have

/home/derix/development/publishing/journal/journal.d

and

/home/derix/development/examples/helloworld/main.d

I work with MonoDevelop. In Project/Project Options/Includes I
specified /home/derix/development/publishing/journal

Building from MonoDevelop spews the following command line

dmd -debug -gc "main.d"  "-I/usr/include/dmd"
"-I/home/derix/development/publishing/journal" "-odobj/Debug"
"-of/home/derix/development/example/helloworld/bin/Debug/example"
-w

and the following error
--- errorlevel 1
obj/Debug/example.o:(.data+0x78): undefined reference to
`_D7Journal12__ModuleInfoZ'


What am I missing ? Or, to put it more simply, what would be the
correct dmd command line to import a module defined in a sibling
directory ?


Thanks,
Dec 27 2014
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Saturday, 27 December 2014 at 17:36:39 UTC, Derix wrote:
 What am I missing ? Or, to put it more simply, what would be the
 correct dmd command line to import a module defined in a sibling
 directory ?
I don't know about the IDE, but the command line is easy: all your projects modules can be listed on it and it will work. dmd yourfile.d journal/journal.d giving them all at once means the necessary code will always be found, compiled, and linked in. It is also generally faster than trying to compile files separately, and easier too - an all around win.
Dec 27 2014
next sibling parent reply "Derix" <derix dexample.com> writes:
But of course ! I should have thought of this myself.
Subsequent question though : what is the -I option for ? The dmd
embedded help states

-Ipath         where to look for imports

so I'd naively think it would work too, but it yields the same
error as above.
Dec 27 2014
next sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Saturday, 27 December 2014 at 18:01:19 UTC, Derix wrote:
 But of course ! I should have thought of this myself.
 Subsequent question though : what is the -I option for ? The dmd
 embedded help states

 -Ipath         where to look for imports

 so I'd naively think it would work too, but it yields the same
 error as above.
The error comes from the linker, the compiler itself found your imported module and compiled it without error. In general, there are two ways in which you can compile a larger project consisting of multiple files. Either compile everything at once, as Adam suggests. In this case, the compiler will also call the linker for you to put all parts together and produce an executable. The other way would be to compile each file (module) separately. To do so, you have to specify the `-c` option; the compiler will then write out an object file for the module you specified on the command line. When you're done compiling all modules, you need to invoke the linker manually, specifying all of the object files. The latter is the default model used in C and C++. The reason is that during development it is often faster to recompile only those parts of your project that have actually changed (and those that depend on them). Compiling D however, is a lot faster than compiling C++, therefore the first method is usually preferrable.
Dec 27 2014
parent "Derix" <derix dexample.com> writes:
OK thanks.
Dec 27 2014
prev sibling parent Mike Parker <aldacron gmail.com> writes:
On 12/28/2014 3:01 AM, Derix wrote:
 But of course ! I should have thought of this myself.
 Subsequent question though : what is the -I option for ? The dmd
 embedded help states

 -Ipath         where to look for imports

 so I'd naively think it would work too, but it yields the same
 error as above.
-I is for compile time, not link time. When the compiler is compling a module, the compiler searches the import path for all the imported modules, loads the files and parses them. This is how it knows which symbols are available in the module it is currently compiling. The compiler does not automatically compile imported modules, so the -I switch has no impact on the linker. Any modules you actually import and use still need to be passed for linking in some manner, either by compiling all the modules at the same time, compiling each individually with -c, linking with a library, or linking with individual object files.
Dec 27 2014
prev sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 27 Dec 2014 17:38:55 +0000
"Adam D. Ruppe via Digitalmars-d-learn"
<digitalmars-d-learn puremagic.com> wrote:

 On Saturday, 27 December 2014 at 17:36:39 UTC, Derix wrote:
 What am I missing ? Or, to put it more simply, what would be the
 correct dmd command line to import a module defined in a sibling
 directory ?
=20 I don't know about the IDE, but the command line is easy: all=20 your projects modules can be listed on it and it will work. =20 dmd yourfile.d journal/journal.d =20 =20 giving them all at once means the necessary code will always be=20 found, compiled, and linked in. It is also generally faster than=20 trying to compile files separately, and easier too - an all=20 around win.
and not always working too. ;-) there is at least one bug with template function attributes inferencing, but as i found no volunteers to narrow it down and i'm using only separate compilation, it will wait for another reporter. ;-)
Dec 27 2014
prev sibling next sibling parent "FrankLike" <1150015857 qq.com> writes:
On Saturday, 27 December 2014 at 17:36:39 UTC, Derix wrote:
 I try to compile the basic HelloWorld.d example in which I only
 added

    import Journal;

 where Journal is a module I defined in Journal.d in another
 directory.

 So I have

 /home/derix/development/publishing/journal/journal.d

 and

 /home/derix/development/examples/helloworld/main.d

 I work with MonoDevelop. In Project/Project Options/Includes I
 specified /home/derix/development/publishing/journal

 Building from MonoDevelop spews the following command line

 dmd -debug -gc "main.d"  "-I/usr/include/dmd"
 "-I/home/derix/development/publishing/journal" "-odobj/Debug"
 "-of/home/derix/development/example/helloworld/bin/Debug/example"
 -w

 and the following error
 --- errorlevel 1
 obj/Debug/example.o:(.data+0x78): undefined reference to
 `_D7Journal12__ModuleInfoZ'


 What am I missing ? Or, to put it more simply, what would be the
 correct dmd command line to import a module defined in a sibling
 directory ?


 Thanks,
If you use the dco,maybe it's ok: at /home/derix/development dco https://github.com/FrankLIKE/dco/ Frank
Dec 28 2014
prev sibling parent "michaladdric" <michaladdric gmail.com> writes:
I too new to this concept but I heard about the line command and
it is easy to learn and implement it.





[url=http://www.redchipsolutions.com/]Lectora elearning
development[/url]
Dec 29 2014