Dynamic loading of shared libraries.

Jens Mueller jens.k.mueller at gmx.de
Mon Jun 13 10:54:26 PDT 2011


Andrei Alexandrescu wrote:
> On 6/13/11 10:33 AM, Jens Mueller wrote:
> >Andrei Alexandrescu wrote:
> >>On 6/13/11 4:27 AM, Jens Mueller wrote:
> >>>Steve Teale wrote:
> >>>>Can DMD D2/Linux do this yet?
> >>>
> >>>dmd can't do it yet. But if all you want is to link a against a shared
> >>>library you can try the following:
> >>>
> >>>1. Create a shared library
> >>>$ gcc -m64 -fPIC -shared shared.c -o libshared.so
> >>>
> >>>2. Building (without linking using dmd)
> >>>$ dmd -m64 -c dynamic.d -ofdynamic.o
> >>>
> >>>3. Use gcc as linker
> >>>$ gcc -m64 dynamic.o /path/to//libphobos2.a -L. -lshared -lrt -o dynamic
> >>>
> >>>4. Execute
> >>>$ ./dynamic
> >>>Hello from shared
> >>>
> >>>I attached the files shared.c and dynamic.d, if you want to try
> >>>yourself.
> >>>
> >>>Jens
> >>
> >>Jens,
> >>
> >>
> >>Two questions - first, what steps do we need to take to convince the
> >>linker call from within dmd to work as above?
> >
> >Never thought about that. Just tried.
> >$ dmd -m64 dynamic.o /path/to/libphobos2.a -L-L. -L-lshared -L-lrt -ofdynamic
> >works.
> 
> Great. Wonder why the path to Phobos is still needed - does using -L
> preclude all implicit uses of it?

No. You're right. It works without the path to Phobos.

> >>Second, how about the converse - loading a shared library from a
> >>program written in either C or D?
> >
> >Don't know exactly what you mean. As far as I know dmd is not able to
> >generate shared libraries. Is that your question?
> 
> Yah, I was wondering if it's just PIC generation or something extra.

There have been some posts regarding support for shared libraries. On
top of my head there we're at least 5 things that need to be done to get
it working.
$ dmd -m64 -L-shared shared.d -oflibshared.so
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o: relocation R_X86_64_32S against `__libc_csu_fini' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
--- errorlevel 1

I do not fully understand the error. But I believe there is something
wrong with dmd generating PIC.

Jens


More information about the Digitalmars-d mailing list