这种 D 到 C 的联系出了什么问题?
我已经设置了一个简单的测试来将 D 代码与 C 链接起来,但我遇到了链接器问题。
// Compiled with "gcc -c CTest.c."
void SayHello()
{
printf("%s", "Hello, world!");
}
// Compiled with "dmd DTest.d CTest.o."
extern (C) void SayHello();
void main()
{
SayHello();
}
ld
吐出:
ld: warning: in CTest.o, file was built for unsupported file format which is not the architecture being linked (i386)
Undefined symbols:
"_SayHello", referenced from:
__Dmain in DTest.o
ld: symbol(s) not found
我尝试使用 -m32 -march=i386
手动指定 CTest.c
的架构,但这给了我一个运行时总线错误。我以前从未遇到过总线错误,所以这让我无法理解。
我做错了什么?
I've set up a trivial test to link D code with C, but I'm running into linker problems.
// Compiled with "gcc -c CTest.c."
void SayHello()
{
printf("%s", "Hello, world!");
}
// Compiled with "dmd DTest.d CTest.o."
extern (C) void SayHello();
void main()
{
SayHello();
}
ld
spits out:
ld: warning: in CTest.o, file was built for unsupported file format which is not the architecture being linked (i386)
Undefined symbols:
"_SayHello", referenced from:
__Dmain in DTest.o
ld: symbol(s) not found
I've tried manually specifying CTest.c
's architecture with -m32 -march=i386
, but that gives me a bus error at runtime. I've never gotten a bus error before, so that just goes right over my head.
What am I doing incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您安装了 32 位 dmd。您最初尝试更改 gcc 似乎是正确的,也许尝试删除 -march 选项。否则尝试使用 -m64 进行 D 编译,如果不起作用,则尝试 -m32。
是的 dmd 可以链接 GCC 生成的针对 Linux ELF 格式的目标文件,我相信 MacOS/BSD?三月格式。 Windows DMD 产生 OMF,而 COFF 现在更流行。
I'm going to assume you have the 32bit dmd installed. Your initial attempt to change gcc seems correct, maybe try dropping the -march option. Otherwise try the D compilation with -m64 then try -m32 if it didn't work.
Yes dmd can link with GCC generated object files targeted at the Linux ELF format, and I believe MacOS/BSD? March format. Windows DMD produces OMF while COFF is more popular now.
Digital Mars 编译器和 GCC 编译器使用不同的目标文件格式。使用(gdc 和 gcc)或(dmd 和 dmc)。
Digital Mars compilers and GCC compilers use different object file formats. Either use (gdc and gcc) or (dmd and dmc).