Results 1 to 9 of 9

Thread: extern "C"

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    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.

  2. #2
    ChimpFace9000
    Guest
    It has to do with the way functions are named. If your using C functions in a C++ program, you use extern "C".

  3. #3
    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.

  4. #4
    ChimpFace9000
    Guest
    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".

  5. #5
    Zaei
    Guest
    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.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  8. #8

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width