Skip to content

Latest commit

 

History

History
20 lines (11 loc) · 1011 Bytes

README.md

File metadata and controls

20 lines (11 loc) · 1011 Bytes

C from C++

How to call C code from C++.

I urge you to use readelf -s on the object files to see what they contain.

Note that the symbols defined by the C++ compiler (thus when using g++ instead of gcc) are much more complicated than those generated by the C compiler.

This is probably the case because C++ has features like templates and classes, so it must add prefixes and suffixes in an specific manner to make the symbols unique.

Therefore, it is much easier to call c from c++ (with a C++ compiler) rather than call c++ from c (with a C compiler). Of course, this is because c++ was made to be backwards compatible with c.

See how g++ links by default to the c standard libraries (for backwards compatibility) since you can simply call functions like printf without linking.

extern "C"

g++ compiles:

  • defined functions to use plain C names in the objects instead of the mangled ones
  • undefined declared functions to depend on the C name instead of the mangled one