View mode: basic / threaded / horizontal-split · Log in · Help
April 25, 2012
D static lib called from C on Mac OS X
Hi,

I am trying to use a D static library from C on Mac OSX Lion, but 
it always fails.

--- file mylib.d ---
module mylib;
import core.runtime;

extern(C) {
	bool mylib_init() {
		return Runtime.initialize();
	}
	
	bool mylib_free() {
		return Runtime.terminate();
	}
}
---

--- file main.c ---
extern void mylib_init();
extern void mylib_free();

int main() {
	mylib_init();
	mylib_free();
	return 0;
}
---

I am compiling using:
$ dmd -c -lib mylib.d -oflibmylib.a
$ gcc -o main main.c -L. -lmylib -lphobos2

When I run ./main, I get a EXC_BAD_ACCESS (SIGSEGV) that seems 
related to thread local storage during runtime initialization.

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   main                          	0x0000000103072dac 
__tls_get_addr + 196
1   main                          	0x0000000103071d80 
thread_attachThis + 312
2   main                          	0x0000000103071c3c thread_init 
+ 24
3   main                          	0x0000000103073296 gc_init + 86
4   main                          	0x0000000103079f79 rt_init + 29
5   main                          	0x00000001030705bb 
D4core7runtime7Runtime10initializeFDFC6object9ThrowableZvZb + 15
6   main                          	0x0000000103069e5d mylib_init 
+ 13
7   main                          	0x0000000103069e2f main + 15
8   main                          	0x0000000103069e14 start + 52

Am I doing something wrong or trying to do something currently 
unsupported?

Thanks,
Nicolas
April 25, 2012
Re: D static lib called from C on Mac OS X
s/void/int in main.c
April 25, 2012
Re: D static lib called from C on Mac OS X
More testing. This:

--- file mylib.d
module mylib;
import core.runtime;
import std.stdio;

extern(C) {
	bool mylib_init() {
		return Runtime.initialize();
	}
	
	bool mylib_free() {
		return Runtime.terminate();
	}

	void mylib_hello() {
		writeln("Hello from mylib");
	}
}

void main() {} // Fake main
---

--- file main.c ---
extern int mylib_init();
extern int mylib_free();
extern void mylib_hello();

int main() {
	mylib_init();
	mylib_hello();
	mylib_free();
	return 0;
}
---

$ dmd -c mylib.d
$ gcc -o main main.c mylib.o -lphobos2 -lpthread -lrt
$ ./main

works on Linux (Unbuntu 11.10), but segfaults on OS X Lion.
April 25, 2012
Re: D static lib called from C on Mac OS X
On 4/25/12, Nicolas Sicard <dransic@gmail.com> wrote:
> --- file main.c ---
> extern void mylib_init();
> extern void mylib_free();

Try changing void to bool there.
April 25, 2012
Re: D static lib called from C on Mac OS X
On Wednesday, 25 April 2012 at 17:59:38 UTC, Andrej Mitrovic 
wrote:
> On 4/25/12, Nicolas Sicard <dransic@gmail.com> wrote:
>> --- file main.c ---
>> extern void mylib_init();
>> extern void mylib_free();
>
> Try changing void to bool there.

This was a typo in my first post. The problem is elsewhere.

Thanks
April 27, 2012
Re: D static lib called from C on Mac OS X
Bug report: http://d.puremagic.com/issues/show_bug.cgi?id=7995
Top | Discussion index | About this forum | D home