|
-
Jan 1st, 2002, 10:41 PM
#1
Thread Starter
Fanatic Member
extern "C"
What is extern "C"? I have seen it here in some code, but I have no idea what it does or is used for.
Alcohol & calculus don't mix.
Never drink & derive.
-
Jan 1st, 2002, 11:39 PM
#2
It has to do with the way functions are named. If your using C functions in a C++ program, you use extern "C".
-
Jan 1st, 2002, 11:50 PM
#3
Member
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.
-
Jan 2nd, 2002, 01:10 AM
#4
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".
-
Jan 2nd, 2002, 07:44 AM
#5
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.
-
Jan 2nd, 2002, 09:23 AM
#6
Monday Morning Lunatic
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).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 6th, 2002, 11:27 AM
#7
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:
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);
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 6th, 2002, 02:58 PM
#8
Thread Starter
Fanatic Member
So basically you use extern "C" to use the same naming convention or whatever you want to call it, and it avoids linker errors?
Alcohol & calculus don't mix.
Never drink & derive.
-
Jan 6th, 2002, 03:15 PM
#9
Monday Morning Lunatic
Yep.
The implicit definition most of the time is extern "C++".
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|