PDA

Click to See Complete Forum and Search --> : extern "C"


Wynd
Jan 1st, 2002, 09:41 PM
What is extern "C"? I have seen it here in some code, but I have no idea what it does or is used for.

ChimpFace9000
Jan 1st, 2002, 10:39 PM
It has to do with the way functions are named. If your using C functions in a C++ program, you use extern "C".

filburt1
Jan 1st, 2002, 10:50 PM
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.

ChimpFace9000
Jan 2nd, 2002, 12:10 AM
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".

Zaei
Jan 2nd, 2002, 06:44 AM
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.

parksie
Jan 2nd, 2002, 08:23 AM
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. FAR = what type of pointer your function is
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).

CornedBee
Jan 6th, 2002, 10:27 AM
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:

/* 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);

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,
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.

Wynd
Jan 6th, 2002, 01:58 PM
So basically you use extern "C" to use the same naming convention or whatever you want to call it, and it avoids linker errors?

parksie
Jan 6th, 2002, 02:15 PM
Yep.

The implicit definition most of the time is extern "C++".