Friday, October 19, 2012

What is Extern "C" in C++?

If you are going to use ctypes in python, then you need to export your dll with a C-wrapper.  

You can find a great definition of extent "C" here.  In case you do not want to click on the link, here is what is said by 'Faisal Vali'.

extern "C" makes a function-name in C++ have 'C' linkage so that client C code can use your function using a 'C' compatible header file that contains just the declaration of your function. Your function definition is contained in a binary format that the client 'C' linker will then link to using the 'C' name.  Since C++ has overloading of function names and C does not, the C++ compiler cannot just use the function name as a unique id to link to, so it mangles the name by adding information about the arguments. A C compiler does not need to mangle the name since you can not overload function names in C. When you state that a function has extern "C" linkage in C++, the C++ compiler does not add argument/parameter type information to the name used for linkage.
I couldn't have said it better myself, so I won't


No comments: