Shared Object with DMD v2.031

teo teo.ubuntu.remove at yahoo.com
Wed Jul 29 23:52:14 PDT 2009


I have difficulties creating a Shared Object (.so) with D. Is it 
possible? Can I use classes defined in the library from the executable?

Here is my library file:
module test; // file "test.d"
export int testMe() { return 1; }
export class Test
{
    private int n;
    this(int i) { n = i; }
    int get() { return n; }
}

I compile like shown below:
$ dmd -fPIC -c test.d
$ gcc -shared -o libtest.so test.o

I have now a "libtest.so" and
$ nm libtest.so
shows following:

00000860 t 
         U _D10ModuleInfo6__vtblZ
         U _D14TypeInfo_Class6__vtblZ
000020b4 V _D20TypeInfo_C4test4Test6__initZ
00002060 D _D4test12__ModuleInfoZ
00000890 T _D4test4Test3getMFZi
00000880 T _D4test4Test6__ctorMFiZC4test4Test
00000904 R _D4test4Test6__initZ
00000924 R _D4test4Test6__vtblZ
00002010 D _D4test4Test7__ClassZ
00000874 T _D4test6testMeFZi
         U _D6Object7__ClassZ
         U _D6object6Object5opCmpMFC6ObjectZi
         U _D6object6Object6toHashMFZk
         U _D6object6Object8opEqualsMFC6ObjectZb
         U _D6object6Object8toStringMFZAya
         U _D9ClassInfo6__vtblZ
         U _D9invariant12_d_invariantFC6ObjectZv
00001f18 a _DYNAMIC
         U _Dmodule_ref
00001ff4 a _GLOBAL_OFFSET_TABLE_
         w _Jv_RegisterClasses
00001f08 d __CTOR_END__
00001f00 d __CTOR_LIST__
00001f10 d __DTOR_END__
00001f0c d __DTOR_LIST__
00000944 r __FRAME_END__
00001f14 d __JCR_END__
00001f14 d __JCR_LIST__
000020c0 A __bss_start
         w __cxa_finalize@@GLIBC_2.1.3
000008b0 t __do_global_ctors_aux
000007a0 t __do_global_dtors_aux
0000200c d __dso_handle
         w __gmon_start__
00000857 t __i686.get_pc_thunk.bx
000020c0 A _edata
000020e0 A _end
000008e8 T _fini
00000724 T _init
000020c0 b completed.6635
000020c4 b dtor_idx.6637
00000820 t frame_dummy


And this is the program:
module main; // file "prog.d"
import std.stdio;
import test;
void main()
{
   writefln("testMe: %d", testMe());
   writefln("Test class: %d", (new Test(3)).get());
}

I compile it with:
$ dmd prog.d -L-L`pwd` -L-ltest

And get:
/usr/bin/ld: dynamic variable `_D4test4Test7__ClassZ' is zero size
/usr/bin/ld: prog.o(.text._Dmain+0x26): unresolvable R_386_32 relocation 
against symbol `_D4test4Test7__ClassZ'
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status
--- errorlevel 1

Please note that "_D4test4Test7__ClassZ" is defined in the library.

BTW compiling with following works:
$ dmd test.d prog.d


More information about the Digitalmars-d-learn mailing list