Compiling to shared library with static dependencies

Relja Ljubobratovic via Digitalmars-d digitalmars-d at puremagic.com
Wed Dec 14 11:36:38 PST 2016


On Wednesday, 14 December 2016 at 12:33:04 UTC, Tanel L wrote:
> Hello,

Hi Tanel,

I have just tried replicating the task you describe, and it 
worked flawlessly for me.

> I am very new to the D world - but serious in moving over to it 
> after I have seen what a cool language it really is.
> I am trying to incorporate D into our main product as an 
> externally loadable module.
>
> Basically I have a minimal project:
>
> ./app.d
> ./helpers/image.d (mir and dcv libraries imported)
> ./helpers/utils.d
>
> I am using the latest (statically compiled) ldc2
>
> In my app file(besides the void main) I have a test function 
> which I am trying to call from python using ctypes:
> extern (C) int doit()

Probably a dumb question - have you tried compiling your library 
without dependencies, and using it through ctypes? Does this work?

>
> Currently I have gotten as far as getting a loading error in 
> python:
> library.so: undefined symbol: _d_eh_personality
>
> Googling disclosed that this is satisfied by libdruntime.so, 
> but I don't have a shared libdruntime anywhere. I guess I have 
> to compile ldc2 in dynamic mode and use the libdruntime.so from 
> there?
>
> My build steps until now:
>
> dub.sdl (pseudocode):
>   project_description "nudesc"
>   dependency "mir"
>   dependency "dcv"
>   targetPath "lib"
>   dflags "-relocation-mode=pic"
>
> dub build --compiler ldc2
> cp .dub/obj/*.o lib/
> cd lib
> ldc2 -of library.so helpers.image.o helpers.utils.o nudesc.o 
> -shared -defaultlib=phobos2

Why are you linking manually? Why not let dub do the job? Also, I 
believe if you're adding dcv as dependency, you don't need to add 
mir, since mir is the dependency of dcv. (it has worked for me 
this way so far) but this has nothing to do with errors you 
describe.

>
> strace shows that ldc2 loads "phobos2" from DMD: 
> "/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libphobos2.so"
>
> The LDC2 folder only seems to contain libphobos2-ldc.a, no 
> .so's.
> Compilation succeeds, but trying to load the so from python:
>
> python  -c "from ctypes import *; CDLL('library.so', 
> mode=RTLD_GLOBAL)"
> OSError: library.so: undefined symbol: _d_eh_personality
>
> I figured that I don't have druntime.so available because 
> druntime is incorporated into LDC2, but mine is statically 
> compiled (downloaded precompiled from github i think).

Again, dumb one (and I'm quite sure this is not the issue) - have 
you initialized druntime in the doit function?

>
> So I tried to compile LDC2 dynamically:
> cmake .. -DBUILD_SHARED=ON
>
> But I get a bunch of these errors before the LDC2 build fails:
> /home/tanel/thirdparty/ldc-dynamic/build/bin/ldc2: error while 
> loading shared libraries: libldc.so: cannot open shared object 
> file: No such file or directory
> runtime/CMakeFiles/druntime-ldc.dir/build.make:93: recipe for 
> target 'runtime/src/core/bitop.o' failed
> make[2]: *** [runtime/src/core/bitop.o] Error 127
>
>
> Basically I am cornered... I really need to be able to compile 
> dcv and mir into my dynamic libraries. Probably LDC2 is a must 
> because DMD and GDC have given me compilation errors on the 
> project even in regular mode. Also LDC2 seems to be the best.
>
> Any ideas what to do or how to proceed?
> Thanks a bunch!

What is your configuration - OS, ldc and gcc version etc.? 
Anyhow, I'm surely not the best person to help here - I hope some 
of more experienced guys will drop a word to help you out...

And for the end, have you tried creating the python module with 
pyd[1]?

Cheers,
Relja

[1] https://github.com/ariovistus/pyd



More information about the Digitalmars-d mailing list