Question

OK, I'm currently playing with D (for the first time), so supposedly I'll be having some ultra-basic question... And here I am... :-)

When I compile-link with dmd it compiles fine :

dmd myMain.d myTestModule.d

When setting the -v (verbose) flag, I noticed the process is basically a compilation step (with -c flag) and linking (with gcc) as usual.

However, when I'm trying compiling the following way, I keep getting errors :

dmd -c MyMain.d myTestModule.d
gcc MyMain.o -o MyMain -m64 -lphobos2 -lpthread -lm

Undefined symbols for architecture x86_64:
  "_D8someFunc3strFmZAya", referenced from:
      __Dmain in MyMain.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

What's going on? Any ideas?

Was it helpful?

Solution

Compiling produces object files that may reference external symbols. Linking is the process of combining group of relevant object files into resulting single binary, taking care of all those external references between them, among other tasks.

Each source file is compiled into one matching object file. In your case, you have compiled myMain.d and myTestModule.d, so there have appeared two object files: myMain.o and myTestModule.o. But you provide only one of them to linker (via gcc) and that predictably results in some symbols unresolved.

gcc myMain.o myTestModule.o -o MyMain -m64 -lphobos2 -lpthread -lm

...should do the trick.

Also note that you can use the very same dmd binary for invoking linker and than it will take care about linking default stuff (phobos, pthread etc.) for you:

dmd myMain.o myTestModule.o -o MyMain -m64

OTHER TIPS

You're also going to need to link in myTestModule.o. dmd -c generates a .o file per .d file. And you're only linking in one of the two that you created.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top