Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't other modules being compiled?

I have two files: Main.d and ImportMe.d. Their purposes should be self-explanatory. They are in the same directory, and have no explicit module declaration. When I try to compile Main.d, though, I get a "symbols not found" error!

$ dmd Main.d -I.

Undefined symbols:
  "_D8ImportMe12__ModuleInfoZ", referenced from:
      _D4Main12__ModuleInfoZ in Main.o
  "_D8ImportMe8SayHelloFxAyaZv", referenced from:
      __Dmain in Main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
--- errorlevel 1

Compiling both files at the same time works fine.

$ dmd Main.d ImportMe.d

You don't have to do this with the standard library, though. What is it doing differently? Changing the include path via -I has no visible effect.

like image 608
Maxpm Avatar asked Jan 18 '23 13:01

Maxpm


2 Answers

When you compile a module, dmd must have the .d or .di files for all of the modules that that module needs in its import path. -I allows you to add paths to the import path. However, that does not build those other modules. It just gives dmd what it needs to build the module that you requested it to build. And when you link, dmd needs either the object files or the library binaries for all of the modules being used in the program, otherwise it's going to complain about undefined symbols (-L can be used for linker flags if you want to link in libraries). The linking step uses the C linker, so it's not D-aware at all and doesn't know anything about modules.

So, if you compile and link in two steps, you first compile each module separately or together with other modules, generating either object files or library files, depending on the flags that you pass the compiler (object files are the default). You then link those object files and libraries together in the linking stage, generating the executable.

When you use dmd without passing it -c or -lib, it's going to do both the compiling and the linking together, so you must provide it all of the modules that you intend to compile, or when it gets to the linking step, it's going to complain about undefined symbols. It doesn't magically go and compile all of the modules that the modules that you ask it to compile import. If you want that sort of behavior, you need to use a tool such as rdmd.

dmd is able to find druntime and Phobos without you having to specify them because of dmd.conf (on Posix) or sc.ini (on Windows). That configuration file adds the appropriate .d and .di files to the import path and adds libphobos.a or phobos.lib (depending on the platform) to DFLAGS so that dmd can find those modules when compiling your modules and can link in the library in the linking phase. It also adds in any other flags that the standard library needs to work (such as linking in librt on Linux). If you move any of those files to non-standard places, it's that configuration file that you need to change to make it so that dmd can still find them.

like image 133
Jonathan M Davis Avatar answered Jan 29 '23 13:01

Jonathan M Davis


You don't have to specify modules from the standard library because the compiler implicitly passes the precompiled standard library .lib file to the linker. For your own projects, consider using rdmd or another build tool.

like image 39
Vladimir Panteleev Avatar answered Jan 29 '23 13:01

Vladimir Panteleev