digitalmars.D - Dynamic loading of shared libraries.
- Steve Teale (1/1) Jun 12 2011 Can DMD D2/Linux do this yet?
- Adam D. Ruppe (2/2) Jun 12 2011 AFAIK, the situation is the same as when you asked last time. Still
- Jens Mueller (16/17) Jun 13 2011 dmd can't do it yet. But if all you want is to link a against a shared
- Andrei Alexandrescu (8/24) Jun 13 2011 Jens,
- Robert Clipsham (8/14) Jun 13 2011 I was under the impression that dmd couldn't produce shared libraries in...
- Jacob Carlborg (6/19) Jun 17 2011 That's correct. And the runtime also will need a couple of changes as
- Jens Mueller (7/39) Jun 13 2011 Never thought about that. Just tried.
- Andrei Alexandrescu (5/43) Jun 13 2011 Great. Wonder why the path to Phobos is still needed - does using -L
- Jens Mueller (13/59) Jun 13 2011 There have been some posts regarding support for shared libraries. On
- Brad Roberts (8/21) Jun 13 2011 When building a shared library, you must build with -fPIC. That's likel...
- Jens Mueller (6/28) Jun 13 2011 Yes. I actually built using -fPIC but pasted the wrong line. Anyway both
- Jacob Carlborg (5/52) Jun 17 2011 The runtime needs a couple of changes as well, for example module info,
- Kai Meyer (9/10) Jun 13 2011 Sure, why not? Just use libdl. I've attached a zip file that should work...
AFAIK, the situation is the same as when you asked last time. Still on the list.
Jun 12 2011
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
On 6/13/11 4:27 AM, Jens Mueller wrote:Steve Teale wrote: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, AndreiCan 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
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=4583Thanks, Andrei-- Robert http://octarineparrot.com/
Jun 13 2011
On 2011-06-13 16:19, Robert Clipsham wrote:On 13/06/2011 14:32, Andrei Alexandrescu wrote: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 CarlborgTwo 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=4583Thanks, Andrei
Jun 17 2011
Andrei Alexandrescu wrote:On 6/13/11 4:27 AM, Jens Mueller wrote:Never thought about that. Just tried. $ dmd -m64 dynamic.o /path/to/libphobos2.a -L-L. -L-lshared -L-lrt -ofdynamic works.Steve Teale wrote:Jens, Two questions - first, what steps do we need to take to convince the linker call from within dmd to work as above?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. JensSecond, 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
On 6/13/11 10:33 AM, Jens Mueller wrote:Andrei Alexandrescu wrote:Great. Wonder why the path to Phobos is still needed - does using -L preclude all implicit uses of it?On 6/13/11 4:27 AM, Jens Mueller wrote:Never thought about that. Just tried. $ dmd -m64 dynamic.o /path/to/libphobos2.a -L-L. -L-lshared -L-lrt -ofdynamic works.Steve Teale wrote:Jens, Two questions - first, what steps do we need to take to convince the linker call from within dmd to work as above?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. JensYah, I was wondering if it's just PIC generation or something extra. AndreiSecond, 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?
Jun 13 2011
Andrei Alexandrescu wrote:On 6/13/11 10:33 AM, Jens Mueller wrote:No. You're right. It works without the path to Phobos.Andrei Alexandrescu wrote:Great. Wonder why the path to Phobos is still needed - does using -L preclude all implicit uses of it?On 6/13/11 4:27 AM, Jens Mueller wrote:Never thought about that. Just tried. $ dmd -m64 dynamic.o /path/to/libphobos2.a -L-L. -L-lshared -L-lrt -ofdynamic works.Steve Teale wrote:Jens, Two questions - first, what steps do we need to take to convince the linker call from within dmd to work as above?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. JensThere 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. JensYah, I was wondering if it's just PIC generation or something extra.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?
Jun 13 2011
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. JensWhen 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
Brad Roberts wrote:On Mon, 13 Jun 2011, Jens Mueller wrote:Yes. I actually built using -fPIC but pasted the wrong line. Anyway both lines result in the same above error.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. JensWhen 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.soThat 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
On 2011-06-13 18:19, Andrei Alexandrescu wrote:On 6/13/11 10:33 AM, Jens Mueller wrote:The runtime needs a couple of changes as well, for example module info, i.e. module constructors, exception handling tables and so on. -- /Jacob CarlborgAndrei Alexandrescu wrote:Great. Wonder why the path to Phobos is still needed - does using -L preclude all implicit uses of it?On 6/13/11 4:27 AM, Jens Mueller wrote:Never thought about that. Just tried. $ dmd -m64 dynamic.o /path/to/libphobos2.a -L-L. -L-lshared -L-lrt -ofdynamic works.Steve Teale wrote:Jens, Two questions - first, what steps do we need to take to convince the linker call from within dmd to work as above?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. JensYah, I was wondering if it's just PIC generation or something extra. AndreiSecond, 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?
Jun 17 2011
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