I'm trying to link D and C code using the DMD and GCC compiler. What I'v tried so far is using the DMD compiler to compile the source to *.o files, using the GCC compiler to compile the C source to *.o files, and using GCC to link the to create the binary.
我尝试使用DMD和GCC编译器来链接D和C代码。到目前为止我所尝试的是使用DMD编译器来编译源到*。o文件,使用GCC编译器编译C源到*。o文件,并使用GCC来链接创建二进制文件。
However, the last step give me linker errors, giving me several "undefined symbol for architecture" errors
但是,最后一步给了我链接器错误,给了我几个“未定义的架构符号”错误。
dmd ../src/Main.d -I../src -c
gcc -c ../ext/clibs.c
gcc *.o -o Main
Undefined symbols for architecture x86_64:
"_D10TypeInfo_k6__initZ", referenced from:
_D11TypeInfo_xk6__initZ in Main.o
"_D12TypeInfo_Aya6__initZ", referenced from:
_D13TypeInfo_xAya6__initZ in Main.o
"_D14TypeInfo_Const6__vtblZ", referenced from:
_D11TypeInfo_xk6__initZ in Main.o
_D13TypeInfo_xAya6__initZ in Main.o
"_D3std5stdio12__ModuleInfoZ", referenced from:
_D4Main12__ModuleInfoZ in Main.o
"__d_arraybounds", referenced from:
_D6object7__arrayZ in Main.o
_D4core4stdc6stdint7__arrayZ in Main.o
_D3std8typecons7__arrayZ in Main.o
_D3std6traits7__arrayZ in Main.o
_D3std9typetuple7__arrayZ in Main.o
"__d_assert", referenced from:
_D6object8__assertFiZv in Main.o
_D4core4stdc6stdint8__assertFiZv in Main.o
_D3std8typecons8__assertFiZv in Main.o
_D3std6traits8__assertFiZv in Main.o
_D3std9typetuple8__assertFiZv in Main.o
"__d_run_main", referenced from:
_main in Main.o
"__d_unittest", referenced from:
_D6object15__unittest_failFiZv in Main.o
_D4core4stdc6stdint15__unittest_failFiZv in Main.o
_D3std8typecons15__unittest_failFiZv in Main.o
_D3std6traits15__unittest_failFiZv in Main.o
_D3std9typetuple15__unittest_failFiZv in Main.o
I'm guessing that the D *.o file refers to symbols in the STD D library. How do I include this when linking?
我猜是D *。o文件是指STD D库中的符号。在链接时如何包括这个?
1 个解决方案
#1
2
Answer I came up with, don't.
Simply use the DMD compiler for the last step
我想出了答案,不要。简单地使用DMD编译器来完成最后一步。
So, Instead of
因此,而不是
dmd ../src/Main.d -I../src -c
gcc -c ../ext/clibs.c
gcc *.o -o Main
Simply
简单的
dmd ../src/Main.d -I../src -c
gcc -c ../ext/clibs.c
dmd *.o
You still have to write a D bridging header listing all the C function you want to use with the extern (C) syntax
您仍然需要编写一个D桥接头,列出您希望使用外部(C)语法使用的所有C函数。
For example
例如
mycfile.c
mycfile.c
int myfunction() {
return 3;
}
mycbridge.d
mycbridge.d
extern (C) int myfunction();
And then include mycbridge.d in your D source.
然后包括mycbridge。d在你的d来源。
#1
2
Answer I came up with, don't.
Simply use the DMD compiler for the last step
我想出了答案,不要。简单地使用DMD编译器来完成最后一步。
So, Instead of
因此,而不是
dmd ../src/Main.d -I../src -c
gcc -c ../ext/clibs.c
gcc *.o -o Main
Simply
简单的
dmd ../src/Main.d -I../src -c
gcc -c ../ext/clibs.c
dmd *.o
You still have to write a D bridging header listing all the C function you want to use with the extern (C) syntax
您仍然需要编写一个D桥接头,列出您希望使用外部(C)语法使用的所有C函数。
For example
例如
mycfile.c
mycfile.c
int myfunction() {
return 3;
}
mycbridge.d
mycbridge.d
extern (C) int myfunction();
And then include mycbridge.d in your D source.
然后包括mycbridge。d在你的d来源。