How to create a shared library with DMD on Linux?

Bradley Smith digitalmars-com at baysmith.com
Tue Jan 9 00:41:48 PST 2007


I'm experimenting with using D for Java Native Interface (JNI) 
programming. Although DMD 1.0 appears to support shared objects on 
Linux, I can't figure out how to create a shared library which can be 
used by a non-D program (in this case java).

Is it possible to create a shared library with DMD which can be used by 
a C program?

If I try to create the shared library with dmd, I get an error on the 
symbol _d_throw at 4.

 > dmd -fPIC nativetest.d jni.d jni_md.d -oflibnativetest.so -L-shared

gcc nativetest.o jni.o jni_md.o -o libnativetest.so -m32 -lphobos 
-lpthread -lm -Xlinker -shared -Xlinker -L/home/basmith/tools/dmd/dmd/lib
/usr/bin/ld: libnativetest.so: undefined versioned symbol name _d_throw at 4
/usr/bin/ld: failed to set dynamic section sizes: Bad value
collect2: ld returned 1 exit status


If I create shared objects with -fPIC and then create a shared library 
with gcc, the library is missing symbols from phobos.


 > dmd -c -fPIC nativetest.d jni.d jni_md.d
 > gcc nativetest.o jni.o jni_md.o -o libnativetest.so -shared
 > java -Djava.library.path=. nativetest
Exception in thread "main" java.lang.UnsatisfiedLinkError: 
/home/basmith/programming/d/djni/libnativetest.so: 
/home/basmith/programming/d/djni/libnativetest.so: undefined symbol: 
_Dmodule_ref
         at java.lang.ClassLoader$NativeLibrary.load(Native Method)
         at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1751)
         at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1676)
         at java.lang.Runtime.loadLibrary0(Runtime.java:822)
         at java.lang.System.loadLibrary(System.java:993)
         at nativetest.<clinit>(nativetest.java:4)


The full code and a build.sh script are in the attached tar.gz file.

Thanks,
   Bradley

-------------- next part --------------
A non-text attachment was scrubbed...
Name: dmdJni.tar.gz
Type: application/gzip
Size: 12239 bytes
Desc: not available
Url : http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20070109/b09529f3/attachment.bin 


More information about the Digitalmars-d-learn mailing list