RE: Can C and C++ object files be linked into an executable?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If all you're trying to do is call "free" functions in the C++ module, simply instruct the compiler to not perform name mangling (use the "extern C { }" linkage spec) and you're off to the races.  Obviously, you can't use references, bool types, or other C++isms as parameters, but you should be good.  I think you'll be ok if you want to access global POD data, too, assuming you watch or (preferably) explicitly specify packing / alignment rules, enum sizes and definitions, bit field defns. (LSB/MSB ordering), etc.  However, C++ objects are NOT guaranteed to be portable - basically, this is dependent on the ABI definitions, and these are not truly standardized.  Usually, if C++ code must be called/used by C code, one writes a C-compliant wrapper layer (using the C++ compiler, extern C stuff) the around the C++ objects/code to cross the ABI boundary.
Hope this helps,
   Mike

-----Original Message-----
From: gcc-help-owner@xxxxxxxxxxx [mailto:gcc-help-owner@xxxxxxxxxxx]On
Behalf Of Ray Hurst
Sent: Friday, January 26, 2007 2:42 PM
To: gcc-help@xxxxxxxxxxx
Subject: Can C and C++ object files be linked into an executable?


Hi,
I have a code that is compiled in C and I need to link in C++ object 
files. I need to know if C++ object files created with a C++ compiler 
can be linked with C object files created with the C compiler.

I have never attempted this. I have either written the entire project in 
C or C++. I have mixed C code with C++ code by placing the C code in a 
namespace. I have also interfaced C++ code to C code by createing a C 
wrapper for the C++ object but everything was compiled with an C++ compiler.
Ray



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux