www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Dynamic loading of shared libraries.

reply Steve Teale <steve.teale britseyeview.com> writes:
Can DMD D2/Linux do this yet?
Jun 12 2011
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
AFAIK, the situation is the same as when you asked last time. Still
on the list.
Jun 12 2011
prev sibling next sibling parent reply Jens Mueller <jens.k.mueller gmx.de> writes:
Content-Disposition: inline

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
Jun 13 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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? Second, how about the converse - loading a shared library from a program written in either C or D? Thanks, Andrei
Jun 13 2011
next sibling parent reply Robert Clipsham <robert octarineparrot.com> writes:
On 13/06/2011 14:32, Andrei Alexandrescu wrote:
 Two questions - first, what steps do we need to take to convince the
 linker call from within dmd to work as above?

 Second, how about the converse - loading a shared library from a program
 written in either C or D?
I was under the impression that dmd couldn't produce shared libraries in its current state - doesn't its output clobber the PIC register or something when compiling with -fPIC? http://d.puremagic.com/issues/show_bug.cgi?id=4583
 Thanks,

 Andrei
-- Robert http://octarineparrot.com/
Jun 13 2011
parent Jacob Carlborg <doob me.com> writes:
On 2011-06-13 16:19, Robert Clipsham wrote:
 On 13/06/2011 14:32, Andrei Alexandrescu wrote:
 Two questions - first, what steps do we need to take to convince the
 linker call from within dmd to work as above?

 Second, how about the converse - loading a shared library from a program
 written in either C or D?
I was under the impression that dmd couldn't produce shared libraries in its current state - doesn't its output clobber the PIC register or something when compiling with -fPIC? http://d.puremagic.com/issues/show_bug.cgi?id=4583
 Thanks,

 Andrei
That's correct. And the runtime also will need a couple of changes as well. Like initializing module info, i.e. module constructors, exception handling tables and so on. -- /Jacob Carlborg
Jun 17 2011
prev sibling parent reply Jens Mueller <jens.k.mueller gmx.de> writes:
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.
 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? Jens
Jun 13 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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?
 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. Andrei
Jun 13 2011
next sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
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
Jun 13 2011
prev sibling next sibling parent Brad Roberts <braddr puremagic.com> writes:
On Mon, 13 Jun 2011, Jens Mueller wrote:

 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
When building a shared library, you must build with -fPIC. That's likely not sufficient to get it actually fully working with dmd. so: dmd -m64 -fPIC -L-shared shared.d -oflibshared.so That said, combining not-well-tested .so support with 64 bit is a good way to discover new bugs, so go for it! :) Later, Brad
Jun 13 2011
prev sibling next sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
Brad Roberts wrote:
 On Mon, 13 Jun 2011, Jens Mueller wrote:
 
 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
When building a shared library, you must build with -fPIC. That's likely not sufficient to get it actually fully working with dmd. so: dmd -m64 -fPIC -L-shared shared.d -oflibshared.so
Yes. I actually built using -fPIC but pasted the wrong line. Anyway both lines result in the same above error.
 That said, combining not-well-tested .so support with 64 bit is a good way 
 to discover new bugs, so go for it! :)
You say not-well-tested .so support. I thought it's still under construction. I'm on dmd 2.053. Jens
Jun 13 2011
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2011-06-13 18:19, 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?
 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. Andrei
The runtime needs a couple of changes as well, for example module info, i.e. module constructors, exception handling tables and so on. -- /Jacob Carlborg
Jun 17 2011
prev sibling parent Kai Meyer <kai unixlords.com> writes:
On 06/12/2011 07:44 AM, Steve Teale wrote:
 Can DMD D2/Linux do this yet?
Sure, why not? Just use libdl. I've attached a zip file that should work like this: $ unzip libdl_example.zip $ ./build_run.sh printer1(0) Valx=5 printer1(0) Valx=5
Jun 13 2011