What is extern "C"? I have seen it here in some code, but I have no idea what it does or is used for.
Printable View
What is extern "C"? I have seen it here in some code, but I have no idea what it does or is used for.
It has to do with the way functions are named. If your using C functions in a C++ program, you use extern "C".
Methinks FAR PASCAL is like extern "C" in that it states the calling convention you use, but that's me talking after writing a "Hello, world" DLL.
The C calling convention and the C++ calling convention are the same. It has to do with the wasy their named.
A c function named "func(void)" ends up as "_func".
a C++ function named "func(void)" ends up something like "@func$qv".
What Chimp Said =).
Actually, C has one calling convention, if i recall correctly, while C++ can have several (__fastcall, __stdcall, __thiscall (member functions), __cdecl, for MSVC).
Z.
FAR = what type of pointer your function isQuote:
Originally posted by filburt1
Methinks FAR PASCAL is like extern "C" in that it states the calling convention you use, but that's me talking after writing a "Hello, world" DLL.
PASCAL = calling convention, normally used in DLLs
Those are a hangover from the 16-bit days, and are obsolete now (they don't work at all in 64-bit XP).
C supports stdcall, cdecl and fastcall, just as C++. The calling conventions are irrelevant here. It's all about internal function names. The names depend on the calling convention in C and on the compiler in C++. For example:
int Func(int i, char c);
C:
_cdecl (the standard):
_Func
_stdcall:
_Func@5 (the number of bytes in the parameter list)
C++:
depends on the compiler, the name basically tells which types the arguments have, like
_i@Func@i_c
It is usually more complicated, but you get the idea. Every compiler has it's own way to create those names.
The point of the thing? If you have a library written and compiled in C, you'll have probably a function named int GetAllOrders(int i, char* c); in there. You used a __stdcall convention. Therefore, in the library the name will be _GetAllOrders@5. Then you write a header for the library:
This works fine as long as you only use it with C. But then you include the file in a c++ program. Let's see the results,Code:/* food.h */
/* This is a header originally written for C */
/* It goes together with food.lib and food.dll */
int __stdcall GetAllOrders(int i, char c);
In the library:
_GetAllOrders@5
The C++ compiler sees:
int _stdcall GetAllOrders(int i, char c);
The C++ compiler creates:
_i#xxstdxx@GetAllOrders@i_c_5@
The linker searches for this name and won't find it, so it creates a linker error. But if you use extern "C" the C++ compiler creates the name following C naming conventions, so the linker will find it.
Phew, that was long. Hope it helped.
So basically you use extern "C" to use the same naming convention or whatever you want to call it, and it avoids linker errors?
Yep.
The implicit definition most of the time is extern "C++".