Using C linkage

When you give a C++ function a name, this is the name that you will use to call the function in your C++ code. However, under the covers, the C++ compiler will decorate the name with extra symbols for the return type and parameters so that overloaded functions all have different names. To C++ developers, this is also known as name mangling.

If you need to export a function through a shared library (in Windows, a dynamic linked library), you must use types and names that other languages can use. To do this, you can mark a function with extern "C". This means that the function has C linkage and the compiler will not use C++ name mangling. Clearly, you should use this only on functions that will be used by external code and you should not use it with functions that have return values and parameters that use C++ custom types.

However, if such a function does return a C++ type, the compiler will only issue a warning. The reason is that C is a flexible language and a C programmer will be able to work out how to turn the C++ type into something usable, but it is poor practice to abuse them like this!

The extern "C" linkage can also be used with global variables, and you can use it on a single item or (using braces) on many items.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset