Search code examples
windowsddmd

DMD: misunderstandings with linking and building


I'm trying to build a project, using the DMD-compiler itself (without IDE) in Windows. And I found myself hardly capable to realise some moments about linking. Usually the IDE does this for me.

The structure of my project

project
├──bin
|   ├──exemple.obj
|   └──exemple.exe
└──src
    ├──a
    |  └──b.d
    └──exemple.d

exemple.d

import a.b;
void  main() { B obg = new B(); }

b.d

module a.b;
class B {
    private int i;
    public this() {i=0;}
    public void act() {i++;}
}

At first it seemed to be easy to build with command:

cd C:\path\to\my\project
dmd bin\exemple.exe src\exemple.d -IC:\path\to\my\project\src

But it only showed me some error-massages:

OPTLINK (R) for Win32  Release 8.00.13
Copyright (C) Digital Mars 1989-2010  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
bin\exemple.obj(exemple)
 Error 42: Symbol Undefined _D1a1b1B7__ClassZ
bin\exemple.obj(exemple)
 Error 42: Symbol Undefined _D1a1b1B6__ctorMFZC1a1b1B
--- errorlevel 2

Finally I guessed that the obj-file was missing. I made it manually with commands:

cd bin
dmd ..\src\a\b.d -c
cd ..

And manually added it to my build-command:

dmd bin\exemple.exe src\exemple.d -IC:\path\to\my\project\src bin\b.obj

And now it works.

Great. But what if we've got lots of additional d-files and complicated folders structure?

How could it be atomised?

I was strongly surprised, when I found out that DMD doesn't doing all this automatically. Maybe, I'm just doing it wrong.


Solution

  • You don't have to build a/b.d separately. But you do have to pass all source (or object) files to dmd. dmd does not figure out the dependencies.

    Have a look at rdmd. It's a tool that does figure out the dependencies and then runs dmd on all of them (and then it runs the executable by default, --build-only prevents that). It comes with the dmd releases.