Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在D中实现C API_C_D - Fatal编程技术网

在D中实现C API

在D中实现C API,c,d,C,D,因此,关于从D中调用capis有很多信息,但是反过来呢?要在D中编写一个与普通C共享库类似的库,您需要做什么?这里有一个简单的例子: main.c extern int foo(int x); void main() { printf("foo(5)=%d\n",foo(5)); } 食品与药物 天真地尝试用gcc和dmd构建和链接这些文件只会导致链接器错误 与gcc main.o foo.o链接: doFoo.o: In function `no symbol': doFoo.d:(

因此,关于从D中调用capis有很多信息,但是反过来呢?要在D中编写一个与普通C共享库类似的库,您需要做什么?这里有一个简单的例子:

main.c

extern int foo(int x);
void main() {
    printf("foo(5)=%d\n",foo(5));
}
食品与药物

天真地尝试用gcc和dmd构建和链接这些文件只会导致链接器错误

与gcc main.o foo.o链接:

doFoo.o: In function `no symbol':
doFoo.d:(.text+0x7): undefined reference to `_Dmodule_ref'
collect2: ld returned 1 exit status
/usr/lib64/libphobos2.a(deh2_2eb_525.o): In function `_D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable':
src/rt/deh2.d:(.text._D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable+0xa): undefined reference to `_deh_beg'
src/rt/deh2.d:(.text._D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable+0x14): undefined reference to `_deh_beg'
src/rt/deh2.d:(.text._D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable+0x1e): undefined reference to `_deh_end'
src/rt/deh2.d:(.text._D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable+0x46): undefined reference to `_deh_end'
/usr/lib64/libphobos2.a(lifetime.o): In function `_D2rt8lifetime18_sharedStaticCtor9FZv':
src/rt/lifetime.d:(.text._D2rt8lifetime18_sharedStaticCtor9FZv+0x15): undefined reference to `_tlsend'
src/rt/lifetime.d:(.text._D2rt8lifetime18_sharedStaticCtor9FZv+0x29): undefined reference to `_tlsstart'
/usr/lib64/libphobos2.a(thread_a3_258.o): In function `_D4core6thread6Thread6__ctorMFPFZvmZC4core6thread6Thread':
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFPFZvmZC4core6thread6Thread+0x2b): undefined reference to `_tlsend'
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFPFZvmZC4core6thread6Thread+0x36): undefined reference to `_tlsstart'
/usr/lib64/libphobos2.a(thread_a3_258.o): In function `_D4core6thread6Thread6__ctorMFDFZvmZC4core6thread6Thread':
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFDFZvmZC4core6thread6Thread+0x28): undefined reference to `_tlsend'
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFDFZvmZC4core6thread6Thread+0x33): undefined reference to `_tlsstart'
/usr/lib64/libphobos2.a(thread_a3_258.o): In function `_D4core6thread6Thread6__ctorMFZC4core6thread6Thread':
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFZC4core6thread6Thread+0x26): undefined reference to `_tlsend'
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFZC4core6thread6Thread+0x31): undefined reference to `_tlsstart'
/usr/lib64/libphobos2.a(thread_a0_713.o): In function `thread_entryPoint':
src/core/thread.d:(.text.thread_entryPoint+0x36): undefined reference to `_tlsend'
src/core/thread.d:(.text.thread_entryPoint+0x41): undefined reference to `_tlsstart'
collect2: ld returned 1 exit status
--- errorlevel 1
与dmd main.o foo.o链接:

doFoo.o: In function `no symbol':
doFoo.d:(.text+0x7): undefined reference to `_Dmodule_ref'
collect2: ld returned 1 exit status
/usr/lib64/libphobos2.a(deh2_2eb_525.o): In function `_D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable':
src/rt/deh2.d:(.text._D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable+0xa): undefined reference to `_deh_beg'
src/rt/deh2.d:(.text._D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable+0x14): undefined reference to `_deh_beg'
src/rt/deh2.d:(.text._D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable+0x1e): undefined reference to `_deh_end'
src/rt/deh2.d:(.text._D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable+0x46): undefined reference to `_deh_end'
/usr/lib64/libphobos2.a(lifetime.o): In function `_D2rt8lifetime18_sharedStaticCtor9FZv':
src/rt/lifetime.d:(.text._D2rt8lifetime18_sharedStaticCtor9FZv+0x15): undefined reference to `_tlsend'
src/rt/lifetime.d:(.text._D2rt8lifetime18_sharedStaticCtor9FZv+0x29): undefined reference to `_tlsstart'
/usr/lib64/libphobos2.a(thread_a3_258.o): In function `_D4core6thread6Thread6__ctorMFPFZvmZC4core6thread6Thread':
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFPFZvmZC4core6thread6Thread+0x2b): undefined reference to `_tlsend'
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFPFZvmZC4core6thread6Thread+0x36): undefined reference to `_tlsstart'
/usr/lib64/libphobos2.a(thread_a3_258.o): In function `_D4core6thread6Thread6__ctorMFDFZvmZC4core6thread6Thread':
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFDFZvmZC4core6thread6Thread+0x28): undefined reference to `_tlsend'
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFDFZvmZC4core6thread6Thread+0x33): undefined reference to `_tlsstart'
/usr/lib64/libphobos2.a(thread_a3_258.o): In function `_D4core6thread6Thread6__ctorMFZC4core6thread6Thread':
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFZC4core6thread6Thread+0x26): undefined reference to `_tlsend'
src/core/thread.d:(.text._D4core6thread6Thread6__ctorMFZC4core6thread6Thread+0x31): undefined reference to `_tlsstart'
/usr/lib64/libphobos2.a(thread_a0_713.o): In function `thread_entryPoint':
src/core/thread.d:(.text.thread_entryPoint+0x36): undefined reference to `_tlsend'
src/core/thread.d:(.text.thread_entryPoint+0x41): undefined reference to `_tlsstart'
collect2: ld returned 1 exit status
--- errorlevel 1

<>如果GCC编译为C++,则外部的默认链接将是C++,而不是C。
extern "C" int foo(int x);

您的D语法似乎没有任何问题。这里有一段话证实了您的方法:

快速浏览一下,
\u Dmodule\u ref
是模块构造函数的链接列表。要解决此问题,请将其添加到
main.c

void* _Dmodule_ref;
程序现在链接并运行良好


(至少,我认为它是这样工作的。)

我的答案是关于使用C中的D静态库。 是的,这有点离题,但D的文档中描述了Windows的共享库(http://www.d-programming-language.org/dll.html)而对于Linux来说,它们仍在建设中(http://www.digitalmars.com/d/2.0/changelog.html). 两个系统的工作示例均附在附件中

  • Win32:dmd+dmc非常有效。例如:

  • Linux32:dmd在找到D main函数后添加了一些必需的内容,因此需要D的main(在Linux32上测试dmd2+gcc)。 它的链接名为“\u Dmain”,不会与C的链接名(真正的“main”)混合。 因此,只需添加带有文本
    void main(){}
    的文件
    dfakemain.d
    dmd-c dfakemain.d
    将创建缺少符号的dfakemain.o。链接到你的目标文件,你会很高兴。例如:


嗯,别让我们吊死。。。错误是什么?链接器错误是:
foo.o:在函数“no symbol”中:foo.d:(.text+0x7):对“\u Dmodule\u ref”的未定义引用
我非常确定,您实际上必须调用来初始化d运行时,而不是仅仅修复链接器错误,但我以前从来没有这样做过,所以我不知道。我知道新闻组中的一些人一直在为Windows GUI应用程序做这件事,所以那里的一些人确切地知道该做什么。我刚刚在D.Learn中发布了一个关于它的问题。希望有人能在这里做出回应。是的,如果你想要像内存分配甚至静态构造函数这样的新奇东西,那么你显然需要设置它:)但是如果你想用d作为更好的C,这可能行得通。这在这种情况下似乎行得通,但我想给它一个更彻底的测试。你知道这个符号应该做什么吗?据我所知,
\u Dmodule\u ref
是模块信息链接列表的标题。在初始化时,模块向列表中添加一个包含模块信息的结构,以便在运行时初始化(内存管理等)完成后,运行时可以调用模块静态/共享构造函数。(至少,我是这样认为的)。C++,不编译为C++。linux方法对我来说很有效,但是它仍然存在一些问题。一个是它要求您使用dmd作为链接器。是否有一些库可以链接以解决该问题?第二,似乎卡在一个假的main中会使该库不能作为一个D库使用,并且可能与使用相同技巧的其他库不兼容。不,它不要求您使用dmd作为链接器(它不能,因为dmd不能链接,它只需要调用一个外部链接器即可)。您只需要链接到
dfakemain.o
。在我的linux示例中,gcc被用作“链接器的调用者”。是否有一些库可以链接以解决该问题?对它是
dfakemain.o
。在示例中,它不与D库(dlibrary.o)链接,也不应该链接。如果它使用一些D库,每个可执行文件应该链接一次。啊,我看到我的答案实际上在你的例子的build.sh中。我丢失了解决方案中的-lrt-lphobos2-lpthread-lm部分。