CodexArcanum
CodexArcanum

Reputation: 4016

Scons not building simple D program in OSX

I'm getting an error when I try to use Scons to build a simple D file. I've tested out scons by building a simple helloworld.c, but the equivalent in D is just not happening and I'm not smart enough about Scons to know if it's a bug or a problem with my setup.

The error I get back is ld: library not found for -lphobos

My SConstruct file:

SConscript('SConscript', variant_dir='release', duplicate=0, exports={'MODE':'release'})
SConscript('SConscript', variant_dir='debug', duplicate=0, exports={'MODE':'debug'})

My SConscript file:

env = Environment()
env.Program(target = 'helloworld', 
            source = ['hello.d'])

hello.d

import std.stdio;

void main() {
  writeln("Hello, world!");
}

EDIT:

The complete scons output is:

MyComputer:thedbook me$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: release debug
gcc -o debug/helloworld debug/hello.o -L/usr/share/dmd/lib -L/usr/share/dmd/src/druntime/import -L/usr/share/dmd/src/phobos -lphobos -lpthread -lm
ld: library not found for -lphobos
collect2: ld returned 1 exit status
scons: *** [debug/helloworld] Error 1
scons: building terminated because of errors.

The reason I'm perplexed is that building the file normally is dead simple (this being the one file helloworld case):

$ dmd hello.d -v
binary    dmd
version   v2.058
config    /usr/bin/dmd.conf
parse     hello
importall hello
... <significant amount of imports here> ...
code      hello
function  D main
function  std.stdio.writeln!(string).writeln
function  std.stdio.writeln!(string).writeln.__dgliteral834
function  std.exception.enforce!(bool,"/usr/share/dmd/src/phobos/std/stdio.d",1550).enforce
gcc hello.o -o hello -m64 -Xlinker -L/usr/share/dmd/lib -lphobos2 -lpthread -lm 

Note that I had to enable verbosity on the compiler to get it to show anything. Normally it would silently build and link the file.

Upvotes: 3

Views: 681

Answers (1)

CodexArcanum
CodexArcanum

Reputation: 4016

After realizing what the output was telling me thanks to a comment, I played around a bit more with the config files and got the result I wanted. I feel like this is a workaround that somewhat undermines the "cruise control" thing that SCons is going for, but I got a build so I can't complain.

I had to modify my SConscript file to look like so:

env = Environment()
target       = 'helloworld'
sources      = ['hello.d']
libraries    = ['phobos2', 'pthread', 'm']
libraryPaths = ['/usr/share/dmd/lib', 
                '/usr/share/dmd/src/druntime/import', 
                '/usr/share/dmd/src/phobos']

env.Program(target = target, 
            source = sources,
            LIBS = libraries,
            LIBPATH = libraryPaths)

The key change here is the addition of LIBS and explicitly writing out which libraries to include rather than relying on SCons to figure it out. Unfortunately, the moment you add one in, you have to explicitly link them all. Still, hope that helps anyone else who wants to get running quickly with D and SCons.

Upvotes: 4

Related Questions