www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static linking on Linux with dmd or gcc

Greetings!  I'm just getting started with D, and just installed 
both dmd and gdc.

Using the Hello, World program as an example, I'm trying to 
create a completely static / self-contained D binary.  Using gdc, 
this is easy to do:

gdc -static hello.d

Then when I ldd hello, the system says:

not a dynamic executable

I can get this same result by creating a C++ program and running 
g++ -static.

However, I can't seem to replicate this behavior with dmd, which 
I'd rather use than gdc.

If I use the command:

gdc -L-static

It passes the "-static" flag to the linker, and not to gcc 
itself, so that doesn't work.

If I use:

dmd -v hello.d

The generated gcc command is this:

gcc hello.o -o hello -m64 -L/usr/lib/x86_64-linux-gnu -Xlinker 
--export-dynamic -l:libphobos2.a -lpthread -lm -lrt

if I modify it to try to get it link statically, so that the 
command looks like this:

gcc -static hello.o -o hello -m64 -L/usr/lib/x86_64-linux-gnu 
-Xlinker l:libphobos2.a -lpthread -lm -lrt

I get this error:

/usr/lib/x86_64-linux-gnu/libphobos2.a(sections_elf_shared_665_420.o): In
function `_D2rt19sections_elf_shared11getTLSRangeFmmZAv':
src/rt/sections_elf_shared.d:(.text._D2rt19sections_elf_shared11getT
SRangeFmmZAv+0x38): undefined reference to `__tls_get_addr'
collect2: error: ld returned 1 exit status

So basically, this Just Works (tm) when using gdc, but I simply 
cannot get it to work when using either dmd directly, or dmd to 
create the object file then using gcc to link.

Is there anyway, using either dmd or dmd together with gcc, I can 
create a completely static binary like gdc can?
Aug 25 2015