编辑:这似乎是一个长期存在的问题,没有迫在眉睫的解决方案:http://d.puremagic.com/issues/show_bug.cgi?id=8133 http://d.puremagic.com/issues/show_bug.cgi?id=8133
http://www.digitalmars.com/d/archives/digitalmars/D/Ideas_for_runtime_loading_of_shared_libraries._154126.html http://www.digitalmars.com/d/archives/digitalmars/D/Ideas_for_runtime_loading_of_shared_libraries._154126.html
http://lists.puremagic.com/pipermail/dmd-internals/2011-December/002853.html http://lists.puremagic.com/pipermail/dmd-internals/2011-December/002853.html
在 C 程序中加载的 d dylib 中启动 d 运行时似乎存在问题。每当我调用 Runtime.initialize() 时,我都会遇到段错误。
C code:
#include <stdio.h>
#include <dlfcn.h>
int main() {
void *library;
int (*fptr)();
library = dlopen("testlib.dylib", RTLD_LAZY);
if(library == NULL)
puts("couldn't load the library");
else {
*(void **)(&fptr) = dlsym(library,"number");
if(fptr == NULL) {
puts("couldn't load function");
}
else {
printf("the result is %d\n",(*fptr)());
}
}
return(0);
}
用 gcc 编译,没有标志。
d code:
import core.runtime;
extern(C) int number() {
Runtime.initialize();
return(4);
}
使用 dmd -shared 编译
回溯:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: 13 at address: 0x0000000000000000
0x000000010003de28 in __tls_get_addr ()
(gdb) bt
#0 0x000000010003de28 in __tls_get_addr ()
#1 0x000000010003cdfc in thread_attachThis ()
#2 0x000000010003ccb8 in thread_init ()
#3 0x000000010003e312 in gc_init ()
#4 0x0000000100044ff5 in rt_init ()
#5 0x000000010003b637 in D4core7runtime7Runtime10initializeFDFC6object9ThrowableZvZb ()
#6 0x0000000100034ee9 in number ()
#7 0x0000000100000e84 in main ()
所有编译和调试均在 OS X 10.7 中完成
注意:如果Runtime.initialize();行被注释掉,库被加载并正确运行。
现在,这个问题已在 github 的最新 druntime 中修复
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)