Thread: What's the purpose of #ifdef __cplusplus?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    What's the purpose of #ifdef __cplusplus?

    I've seen the following in numerous header files.

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    // some declarations
    
    #ifdef __cplusplus
    }
    #endif
    Presumably if the code is compiled as C++ the 'extern "C"' bit get's included in the header, but that does that achieve?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To ensure that the names declared in that portion of code have C linkage, and thus C++ name mangling is not performed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thank you!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It tells the C++ compiler to use C-style linkage. It avoids name mangling any extern "C" function (which it normally uses to deal with function overloading), so regular C programs can use the functions too. More info here: In C++ source, what is the effect of extern "C"? - Stack Overflow

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Another usage(the one that i have come in face with) of the
    Code:
    extern "C"
    {
    ..
    }
    is that someone has written a library in C,but your project is in C++,then with this extern you can use the C library into your C++ project

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That's not another usage, you're discussing the same thing everyone else is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little doubt regarding #ifdef __cplusplus
    By rockubaby in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2010, 07:25 PM
  2. MinGW - __cplusplus not being defined?
    By Mario F. in forum Tech Board
    Replies: 5
    Last Post: 12-04-2006, 07:17 PM
  3. #ifdef - Can It Be Used With Logic Such as OR / AND?
    By dedham_ma_man in forum C Programming
    Replies: 3
    Last Post: 04-21-2006, 02:57 PM
  4. #ifdef and so on.
    By MipZhaP in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 05:34 PM
  5. How To use #ifdef ?
    By GaPe in forum Windows Programming
    Replies: 1
    Last Post: 11-01-2003, 12:57 PM