extern "C" question.
-
If C++ already supports the C Standard Library, why do we need to declare extern "C" when using C-linkage? When do we use extern "C"? Is this compiler-specific feature? Or is it defined in the standard ANSI C++? On MSDN, it says Microsoft C++ supports the strings "C" and "C++" in the string-literal field. However, I have never seen something like extern "C++". Has anyone seen it? Will we ever need to use extern "C++"? Thanks
-
If C++ already supports the C Standard Library, why do we need to declare extern "C" when using C-linkage? When do we use extern "C"? Is this compiler-specific feature? Or is it defined in the standard ANSI C++? On MSDN, it says Microsoft C++ supports the strings "C" and "C++" in the string-literal field. However, I have never seen something like extern "C++". Has anyone seen it? Will we ever need to use extern "C++"? Thanks
Hello Alex, The extern "C" declaration is used to tell the C++ compiler not to mangle the symbol which is being declared. When the C++ compiler encounters a function name like "DisplayCurrentThreadId()" which has not been declared as extern "C", it will emit a symbol for it which will look like the following : ?DisplayCurrentThreadId@@YAXXZ The actual symbol produced depends on the compiler used (the above was produced by VC++ 6.0). This symbol will be used in the resulting OBJ file for linking purposes. C++ has a variety of reasons for symbol name mangling, of course. But the reason for the mangling of function name (both class functions and global ones) is to enable function name overloading (i.e. using the same function name with different parameter types). If extern "C" was used to declare the function, the symbol produced for it could be : _DisplayCurrentThreadId This depends on the compiler used. But the bottom line is that no function name overloading will be allowed (since the C language does not support this, hence extern "C"). Hope this helps. Best Regards, Bio.
-
Hello Alex, The extern "C" declaration is used to tell the C++ compiler not to mangle the symbol which is being declared. When the C++ compiler encounters a function name like "DisplayCurrentThreadId()" which has not been declared as extern "C", it will emit a symbol for it which will look like the following : ?DisplayCurrentThreadId@@YAXXZ The actual symbol produced depends on the compiler used (the above was produced by VC++ 6.0). This symbol will be used in the resulting OBJ file for linking purposes. C++ has a variety of reasons for symbol name mangling, of course. But the reason for the mangling of function name (both class functions and global ones) is to enable function name overloading (i.e. using the same function name with different parameter types). If extern "C" was used to declare the function, the symbol produced for it could be : _DisplayCurrentThreadId This depends on the compiler used. But the bottom line is that no function name overloading will be allowed (since the C language does not support this, hence extern "C"). Hope this helps. Best Regards, Bio.
-
Bio, Thanks for your reply. I have one more question, does the term extern "C" has anything to do with the storage specifier extern? Now that I understand name mangling, I wonder if extern "C" is a C++ language feature, or is it compilers' feature?
Alex Ngai wrote: does the term extern "C" has anything to do with the storage specifier extern? no, not at all. Alex Ngai wrote: I wonder if extern "C" is a C++ language feature, or is it compilers' feature? C++ standard specification.
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
If C++ already supports the C Standard Library, why do we need to declare extern "C" when using C-linkage? When do we use extern "C"? Is this compiler-specific feature? Or is it defined in the standard ANSI C++? On MSDN, it says Microsoft C++ supports the strings "C" and "C++" in the string-literal field. However, I have never seen something like extern "C++". Has anyone seen it? Will we ever need to use extern "C++"? Thanks
extern C++ is defined in the standard to mean do whatever the compilers default C++ is. since this is the default it is almost never used.