Thread: extern "C"

  1. #1
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246

    extern "C"

    hi, i was reading some code and came across this in a header file:
    Code:
          #ifdef __cplusplus
          extern "C" {
          #endif
    
          ...
    
         #ifdef __cplusplus
         }
         #endif
    what does that mean? It's there so that the code can be compiled as c++?
    :wq

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It's there so that the code can be compiled as c++?
    No, its so some C++ code can call some other code which was compiled with C

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    uh, yes..

    c++ compilers will define __cplusplus automatically

    and if it's defined it gets compiled everything between #ifdef __cplusplus and #endif
    will get compiled.

    the extern "C" prevents name mangling of functions
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    so, if i write a c++ prog that calls some functions from this library (it's a string library source code), it will work? and if the extern "C" is not there it won't?
    :wq

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Its there because of something called name mangling. Lets say you have a header like this:

    Example:
    Code:
    #ifndef MY_HEADER
    #define MY_HEADER 1
    
    void draw_screen(void);
    void process_input(int key);
    
    #endif
    Your C compiler will call those functions _draw_screen and _process_input, respectively.

    Now your C++ compiler may desire to call them _draw_screen10239814023984@LYWEORuijk02394u8 and _process_input203948kl2430@a029384LKJSf09234.

    By changing the header to this:


    Example:
    Code:
    #ifndef MY_HEADER
    #define MY_HEADER 1
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    void draw_screen(void);
    void process_input(int key);
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif
    You are telling your C++ compiler that it needs to link to _draw_screen, not _draw_screen10239814023984@LYWEORuijk02394u8.

  6. #6
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    that's a nice explanation, it's clear now, thanks
    :wq

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    > so, if i write a c++ prog that calls some functions from this library (it's a string library source code), it will work? and if the extern "C" is not there it won't?

    Perhaps, but given the fact that I'm willing to bet that this library is written in C, no. Not only that, but its good practice to make sure you use extern "C" in libraries so that the functions have a predictable name. However its not really required since compiled libraries are not usually portable, you would just recompile the library with the new compiler. Name mangling varies from compiler to compiler. If the library is meant to be compiled in C, you will have to use the extern "C" no matter what when calling it from the C++ code since otherwise your C++ code will export calls to the name mangled version of the function. Your linker will get lost and die.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extern "C" problem
    By CodeBugs in forum C++ Programming
    Replies: 11
    Last Post: 06-10-2009, 09:14 AM
  2. error C2059 with extern "C"
    By Elysia in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2008, 06:16 PM
  3. extern "C"
    By George2 in forum C++ Programming
    Replies: 19
    Last Post: 02-08-2008, 02:33 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Unsure of how to properly use extern "C"
    By INFERNO2K in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2005, 11:54 AM