www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - DUB fails to build a dynamic library on Linux

reply "Gabor Mezo" <gabor.mezo outlook.com> writes:
Hello,

I've created a simple db dynamic lib project.

dub.json:

{
	"name": "node-d-sample",
	"description": "A minimal D application.",
	"copyright": "Copyright © 2014, gabor",
	"authors": ["gabor"],
	"dependencies": {
	},
	"libs": [ "phobos2" ],
	"targetName": "node-d-sample",
	"targetPath": "lib",
	"targetType": "dynamicLibrary"
}

I have only one source, lib.d:

extern (C)
{
	int ping()
	{
		return 555;
	}
}

My system is a Mint x64, dub latest, dmd latest.

If I invoke "dub", then:

Building node-d-sample ~master configuration "library", build 
type debug.
Compiling using dmd...
Linking...
/usr/bin/ld: 
.dub/build/library-debug-linux.posix-x86_64-dmd_2066-44F775BF77E519551012EFF79429B
A3/node-d-sample.o: 
relocation R_X86_64_32 against `.rodata' can not be used when 
making a shared object; recompile with -fPIC
.dub/build/library-debug-linux.posix-x86_64-dmd_2066-44F775BF77E519551012EFF79429B
A3/node-d-sample.o: 
error adding symbols: Bad value
collect2: error: ld returned 1 exit status
--- errorlevel 1
FAIL 
.dub/build/library-debug-linux.posix-x86_64-dmd_2066-44F775BF77E51
551012EFF79429BBA3/ 
node-d-sample dynamicLibrary
Error executing command run: dmd failed with exit code 1.
Dec 14 2014
next sibling parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
You need to add `-fPIC` to your compiler flags. In dub.json:

     ...,
     "dflags": ["-fPIC"]
Dec 14 2014
prev sibling parent reply Martin Nowak <code+news.digitalmars dawg.eu> writes:
On 12/14/2014 03:50 PM, Gabor Mezo wrote:
 Hello,

 I've created a simple db dynamic lib project.
https://github.com/D-Programming-Language/dub/issues/352
Dec 14 2014
parent reply "Gabor Mezo" <gabor.mezo outlook.com> writes:
On Sunday, 14 December 2014 at 16:06:25 UTC, Martin Nowak wrote:
 On 12/14/2014 03:50 PM, Gabor Mezo wrote:
 Hello,

 I've created a simple db dynamic lib project.
https://github.com/D-Programming-Language/dub/issues/352
Thanks, I've did this, it finally build, but something is still not good. If I call externals from another application, and my D code allocates anything on heap, the calling process crashes with SIGSEV. I mean it works: extern (C) { int ping() { return 555; } } But it doesn't: extern (C) { int ping() { try { throw new Exception("foo"); } catch (Exception ex) { } return 555; } } or this one crashes too: extern (C) { int ping() { auto foo = new Object(); return 555; } }
Dec 14 2014
next sibling parent "Gabor Mezo" <gabor.mezo outlook.com> writes:
I've opened an other thread instead, because this is a different
issue. It's there:

http://forum.dlang.org/thread/aycvoeaurrpmuehatdwp forum.dlang.org#post-aycvoeaurrpmuehatdwp:40forum.dlang.org
Dec 14 2014
prev sibling parent reply ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Sun, 14 Dec 2014 16:24:13 +0000
Gabor Mezo via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 On Sunday, 14 December 2014 at 16:06:25 UTC, Martin Nowak wrote:
 On 12/14/2014 03:50 PM, Gabor Mezo wrote:
 Hello,

 I've created a simple db dynamic lib project.
https://github.com/D-Programming-Language/dub/issues/352
=20 Thanks, =20 I've did this, it finally build, but something is still not good. =20 If I call externals from another application, and my D code=20 allocates anything on heap, the calling process crashes with=20 SIGSEV.
did you called `rt_init()` for your dynamic library? you have to do that in the calling process. and don't forget to call `rt_term()` on exiting.
Dec 14 2014
parent reply "Gabor Mezo" <gabor.mezo outlook.com> writes:
Thank you for your quick help, that was it.

I do (or at least I'm trying to do) hobbyist D programming as you 
guys, that's why I come here to cry at Sunday evening. :)

Ok, I'm almost there. It builds with DUB/DMD, I can allocate 
stuff on the heap, so far so good. But if I invoke:

dub --compiler=gdc

then I got:

Performing main compilation...
dub build "node-d-sample" --arch=x86_64 --compiler=gdc 
"--build=release"
Building package node-d-sample in 
/home/gabor/sandbox/node-d-sample/
Building node-d-sample ~master configuration "library", build 
type release.
Running gdc...
/usr/bin/ld: 
/usr/lib/gcc/x86_64-linux-gnu/4.9/libgphobos2.a(minfo.o): 
relocation R_X86_64_32 against 
`_D32TypeInfo_APxS6object10ModuleInfo6__initZ' can not be used 
when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.9/libgphobos2.a: error adding 
symbols: Bad value
collect2: error: ld returned 1 exit status
FAIL 
.dub/build/library-release-linux.posix-x86_64-gdc_2065-33A38D9D3DC71
C1501A18C957A2B35B/ 
node-d-sample dynamicLibrary
Error executing command build: gdc failed with exit code 1.

Exit code 2
Build complete -- 1 error, 0 warnings

or:

dub --compiler=ldmd2

then I got:

Performing main compilation...
dub build "node-d-sample" --arch=x86_64 --compiler=ldmd2 
"--build=release"
The determined compiler type "ldc" doesn't match the expected 
type "dmd". This will probably result in build errors.
Building package node-d-sample in 
/home/gabor/sandbox/node-d-sample/
Building node-d-sample ~master configuration "library", build 
type release.
Running ldmd2...
/usr/bin/ld: 
/home/gabor/dev/ldc2-0.15.0-beta1-linux-x86_64/bin/../lib/libd
untime-ldc.a(eh.o): 
relocation R_X86_64_32 against `.rodata..str1' can not be used 
when making a shared object; recompile with -fPIC
/home/gabor/dev/ldc2-0.15.0-beta1-linux-x86_64/bin/../lib/libdruntime-ldc.a: 
error adding symbols: Bad value
collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1
FAIL 
.dub/build/library-release-linux.posix-x86_64-ldc_2066-212B345732639
80C27025028ED586A2/ 
node-d-sample dynamicLibrary
Error executing command build: ldmd2 failed with exit code 1.

Exit code 2
Build complete -- 1 error, 0 warnings

It seems DUB somehow eats dflags away for non DMD compilers. How 
the hell should I prevent this happen? Thanks.
Dec 14 2014
next sibling parent reply ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Sun, 14 Dec 2014 16:57:20 +0000
Gabor Mezo via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 Thank you for your quick help, that was it.
=20
 I do (or at least I'm trying to do) hobbyist D programming as you=20
 guys, that's why I come here to cry at Sunday evening. :)
ah, that's ok. but you'd better ask your questions in D.learning. not 'cause you're a beginner, but just 'cause D.learning has invalid name. it should be D.questions. besides, when people see the question in "general", they tend to give shorter and less detailed answers than in "learning". sorry for the noise, as i can't tell you anything about DUB.
Dec 14 2014
parent "Gabor Mezo" <gabor.mezo outlook.com> writes:
Actually I'm practicing D for about three months but this C 
interfacing mumbo jumbo is still missing for me. Ok, I'm go there 
if I'm stuck, thanks.
Dec 14 2014
prev sibling parent "David Nadlinger" <code klickverbot.at> writes:
On Sunday, 14 December 2014 at 16:57:24 UTC, Gabor Mezo wrote:
 /home/gabor/dev/ldc2-0.15.0-beta1-linux-x86_64/bin/../lib/libd
untime-ldc.a(eh.o): 
 relocation R_X86_64_32 against `.rodata..str1' can not be used 
 when making a shared object; recompile with -fPIC
Hm, the problem here is that LDC uses a static runtime build, while dynamic libraries require Phobos and druntime to be built as shared libraries. The latter has actually been supported since quite a few months now, but IIRC still isn't shipped as part of the binary packages: https://github.com/ldc-developers/ldc/issues/807 If you want to give it a try, you can quickly build LDC from source (http://wiki.dlang.org/Building_LDC_from_source) and pass the -DBUILD_SHARED_LIBS=ON flag to CMake. Building shared libraries should then succeed. David
Dec 14 2014