If you are a seasoned system programmer, it is very likely that you had to write or maintain mixed C/C++ application. And if you had, then you know that if you want to make functions in the translation unit compiled from C source available for C++ code, you must delcare them as extern "C". The same applies to functions implemented in C++, which you want to make available to C programs. Usually this is is done with preporcessor in that way (YMMV though):
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C extern
#endif
EXTERN_C void c_style_function(void);
You need extern "C" in C++, because otherwise C++ compiler will mangle function symbol, so it will be iaccessible from code compiled from C. The rational behind C++ mangling is that you may overload functions (to put it simply, functions with the same name but different parameter lists may have different implementation).
However I was very surprised when I found that Microsoft C/C++ compiler mangles data symbols. Back then I though: "Why on Earth, they do that? You cannot overload variables!".
Recently I read an article about dynamic shared objects on Linux and on page 32 I found the reason behind that "strange" mangling. It is actually done to help (but does not ensure though) developers preserve ABI compatibility between several versions of a library. If you change data layout, old applications that relied on particular data layout will crash the game. Data object mangling helps you to find some of these errors in compile-time, instead of run-time.
Posted in Software engineering frolov's blog | add new comment
Submitted by frolov on Wed, 2005-04-13 11:07.



