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?
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.