If I compile a file importing etc.c.zlib into a shared library as
rdmd --build-only -shared -fPIC -lphobos2 testsharedlib.d,
I get this when trying to load the library:
Fatal Error while loading '/home/lomereiter/dmd2/linux/lib64/libphobos2.so.0.64':
The module 'etc.c.zlib' is already defined in './testsharedlib'.
---
testsharedlib.d:
import etc.c.zlib;
void main() {}
---
loading from Python:
import ctypes
lib = ctypes.CDLL('./testsharedlib')
Comment #1 by lomereiter — 2013-10-27T14:56:26Z
AFAIU DMD 2.064 should come with a shared version of Zlib library, because otherwise its symbols become duplicated, and module collision detector prevents loading user-produced shared libraries (that behaviour was introduced in https://github.com/D-Programming-Language/druntime/pull/487)
So I mark that as a regression, 'cause I don't wanna be stuck with buggy 2.063 shared library support.
Comment #2 by bugzilla — 2013-10-27T22:59:45Z
Don't link in the unshared version of phobos when building a shared library, otherwise you'll get multiple definitions of the same symbols.
Any shared libs you create should be linked against libphobos2.so, not -lphobos2.
Comment #3 by lomereiter — 2013-10-28T01:19:20Z
But it is linked against _shared_ version. Moreover, it is impossible to link against libphobos2.a because it's not compiled with -fPIC.
I have to specify -L-lphobos2 on the command line just for that, otherwise DMD tries to use static libphobos2.a, and linker barks that it should be recompiled with -fPIC
> /usr/bin/ld: /home/artem/dmd2/linux/bin64/../lib64/libphobos2.a(lifetime_46c_6c8.o): relocation R_X86_64_32 against `_D15TypeInfo_Shared7__ClassZ' can not be used when making a shared object; recompile with -fPIC
> /home/artem/dmd2/linux/bin64/../lib64/libphobos2.a: could not read symbols: Bad value
> collect2: ld returned 1 exit status
Comment #4 by code — 2013-10-29T20:30:37Z
Seems like rdmd includes the etc in the build so the error message is correct because the etc.c.zlib module is already defined in libphobos2.so.
Try the --exclude=etc option to avoid this.
Comment #5 by code — 2013-10-29T20:32:23Z
(In reply to comment #4)
> Seems like rdmd includes the etc in the build so the error message is correct
> because the etc.c.zlib module is already defined in libphobos2.so.
> Try the --exclude=etc option to avoid this.
The other part of the story is that rdmd should know about this and not build etc.c phobos modules.
Comment #6 by lomereiter — 2013-10-29T23:47:13Z
(In reply to comment #4)
> Seems like rdmd includes the etc in the build so the error message is correct
> because the etc.c.zlib module is already defined in libphobos2.so.
> Try the --exclude=etc option to avoid this.
Thanks, that works. But such behavior of rdmd is quite confusing to the users; speaking for myself, I never had a use case for the --exclude option before.
Comment #7 by code — 2013-10-30T13:41:42Z
OK I renamed the bug and lowered the priority.
Sadly there is no tools Component in this Bugzilla.