On 2012-04-08 10:45, "Timo Westkämper" <timo.westkam...@gmail.com>" wrote:
I am still testing which setup gives me reliable shared D libraries
which can be used from C.

Here is my latest test:

* test.d:
import std.stdio;
extern (C) {
void hiD() {
writeln("hi from D lib");
}
}

* main.c
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
void main() {
void (*hiD)(void);
void* handle = dlopen("./libtest.so", RTLD_LAZY);
if (handle == NULL) {
printf("%s\n", dlerror());
exit(1);
}
hiD = dlsym(handle, "hiD");
if (hiD != NULL) {
hiD();
} else {
printf("hiD is null\n");
}
dlclose(handle);
}

* Makefile
#!/bin/bash
test:
#gdc-4.6 -g -c test.d -fPIC -o test.o
#gdc-4.6 -shared -o libtest.so -fPIC test.o -lc -nostartfiles
dmd -g -c test.d -fPIC
ld -shared -o libtest.so test.o -lrt -lphobos2 -lpthread
gcc -g main.c -ldl -lpthread
./a.out
clean:
rm -rf *.so *.o *.out

With this setup I get

./libtest.so: undefined symbol: _deh_beg

With a fake main method added I get

make: *** [test] Segmentation fault

This is what I get from gdb

Program received signal SIGSEGV, Segmentation fault.
0xb7fd1ed3 in std.stdio.__T7writelnTAyaZ.writeln() (_param_0=...) at
/usr/include/d/dmd/phobos/std/stdio.d:1550
1550 enforce(fprintf(.stdout.p.handle, "%.*s\n",

Event with the following startup hooks

extern(C) {

void gc_init();
void gc_term();

void _init() {
gc_init();
}

void _fini() {
gc_term();
}

}

I get the same error. I am using dmd 2.058 on Ubuntu 11.10 (32 bit)

With gdc I get different errors, but it seems even more difficult to get
it working.

Does anyone know what is missing to get proper shared library support
working on Linux?

This is what I can think of for now:

* Proper initialization of TLS data
* Setting up exception handling tables
* Setting up module info

--
/Jacob Carlborg

Reply via email to