gpt4 book ai didi

c - 在 D 中实现 C API

转载 作者:太空狗 更新时间:2023-10-29 16:29:41 24 4
gpt4 key购买 nike

所以有很多关于从 D 中调用 C API 的信息,但是反过来呢?要用 D 编写一个像普通 C 共享库一样工作的库,您需要做什么?这是一个简单的案例:

主.c

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

foo.d

extern(C)
{
int foo(int x)
{
return x*x;
}
}

天真地尝试使用 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

与 dmd main.o foo.o 链接:

/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

最佳答案

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

  • Win32:dmd+dmc 运行良好。 示例:test_d_from_c_win32.zip

  • Linux32:dmd 在找到 D main 函数后添加了一些必需的东西,因此需要 D 的 main(在 Linux32 上测试 dmd2+gcc)。它的链接名称是“_Dmain”,不会与 C 的名称(真正的“main”)混合。因此,只需添加文本 void main(){} 的文件 dfakemain.ddmd -c dfakemain.d 将创建缺少符号的 dfakemain.o。将它与您的目标文件链接起来,您会很高兴。 示例:test_d_from_c_linux32.tar.gz

关于c - 在 D 中实现 C API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7480046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com