This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: Can C and C++ object files be linked into an executable?
- From: "Young, Michael" <Michael dot Young at paetec dot com>
- To: "Ray Hurst" <rhurst2 at cox dot net>, <gcc-help at gcc dot gnu dot org>
- Date: Fri, 26 Jan 2007 14:55:59 -0500
- Subject: RE: Can C and C++ object files be linked into an executable?
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@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]On
Behalf Of Ray Hurst
Sent: Friday, January 26, 2007 2:42 PM
To: gcc-help@gcc.gnu.org
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