3

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?

5
  • 1
    well.. unsuported file format.. not to be captain obvious here, but try compiling with D fronted for gcc (gdc i think)
    – fazo
    Oct 17, 2011 at 18:49
  • @fazo Why would that be necessary? The D website says it can be done without much additional effort.
    – Maxpm
    Oct 17, 2011 at 19:21
  • it's not about c<->c++ interface but interoperability between D and c/c++ objects produced with incompatible compilers.
    – fazo
    Oct 17, 2011 at 20:45
  • FWIW, doing exactly as you have done on OSX 10.6.8, gcc 4.2.1, dmd 2.055 works fine for me. Oct 17, 2011 at 21:31
  • @PeterAlexander That's very odd; I'm using the exact same versions as you are! Why would that happen?
    – Maxpm
    Oct 18, 2011 at 2:11

2 Answers 2

5

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.

3
  • Dropping -march did the trick! Do I dare ask why that worked?
    – Maxpm
    Oct 18, 2011 at 2:24
  • Here is a lot more information than you ever wanted to know regarding linking D to C. The main thing that bit me was Thread Local Storage. Hope you find something useful. stackoverflow.com/q/7774365/978992
    – 1100110
    Oct 18, 2011 at 4:10
  • I am unsure what -march is supposed to do (optimization?) but I never used it myself. Oct 18, 2011 at 14:39
-3

Digital Mars compilers and GCC compilers use different object file formats. Either use (gdc and gcc) or (dmd and dmc).

4
  • Can I work around this by putting it in a "formal" library, i.e. libFoo.a?
    – Maxpm
    Oct 17, 2011 at 19:39
  • Not sure but I doubt it. But dynamically linked libraries (.dll, or .so) should probably work.
    – cyco130
    Oct 17, 2011 at 19:41
  • Unless I'm doing something wrong again, dynamic libraries don't work, either. Same error.
    – Maxpm
    Oct 17, 2011 at 20:58
  • 3
    DMD only produces different formats on Windows. Archives are still the same format, shared I think just has additional requirements. Oct 17, 2011 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.