Re: D static lib called from C on Mac OS X

2012-04-27 Thread Nicolas Sicard

Bug report: http://d.puremagic.com/issues/show_bug.cgi?id=7995



D static lib called from C on Mac OS X

2012-04-25 Thread Nicolas Sicard

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  	0x000103072dac 
__tls_get_addr + 196
1   main  	0x000103071d80 
thread_attachThis + 312
2   main  	0x000103071c3c thread_init 
+ 24

3   main0x000103073296 gc_init + 86
4   main0x000103079f79 rt_init + 29
5   main  	0x0001030705bb 
D4core7runtime7Runtime10initializeFDFC6object9ThrowableZvZb + 15
6   main  	0x000103069e5d mylib_init 
+ 13

7   main0x000103069e2f main + 15
8   main0x000103069e14 start + 52

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


Thanks,
Nicolas




Re: D static lib called from C on Mac OS X

2012-04-25 Thread Nicolas Sicard

s/void/int in main.c


Re: D static lib called from C on Mac OS X

2012-04-25 Thread Nicolas Sicard

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.




Re: D static lib called from C on Mac OS X

2012-04-25 Thread Andrej Mitrovic
On 4/25/12, Nicolas Sicard dran...@gmail.com wrote:
 --- file main.c ---
 extern void mylib_init();
 extern void mylib_free();

Try changing void to bool there.


Re: D static lib called from C on Mac OS X

2012-04-25 Thread Nicolas Sicard
On Wednesday, 25 April 2012 at 17:59:38 UTC, Andrej Mitrovic 
wrote:

On 4/25/12, Nicolas Sicard dran...@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