Help with __cplusplus

Discussion in 'C Programming' started by Arturo, Jun 9, 2004.

  1. Arturo

    Arturo Guest

    Hello Netlanders, I'm starting to learn the 'C' language
    and reading some code I found this code:

    #ifdef __cplusplus
    extern "C" {
    #endif

    Is there someone who could explain me about this code, because
    I see that this "__cplusplus" and "extern "C" {" is a kind of
    standard to see the difference between C and C++, but I'm not sure
    and I want more Information ...

    If you know about this stuff or about a reading to consult please
    let me know it.
     
    Arturo, Jun 9, 2004
    #1
    1. Advertisements

  2. This is a feature of C++. The code you are looking at is designed for a
    compiler that works as a C or C++ compiler. If you set the switches for C
    the #ifdef is false and the extern is used. If you set the switches for
    C++ the #ifdef is true and it uses the extern as required by C++. If you
    are interested in more information ask the guys in comp.lang.c++ what the
    'extern "C"' is all about or check out a book on C++.
     
    Darrell Grainger, Jun 9, 2004
    #2
    1. Advertisements

  3. Arturo

    osmium Guest

    It looks like you have encountered the header of a library function and the
    vendor that supplies the compiler uses the same library for a C++ compiler.
    This is the norm, nowadays. __cplusplus means something to *your* compiler
    (it can't mean anything in the C standard because time only seems to flow in
    the forward direction and C preceded C++). The syntax extern"C" { is a way
    to defeat name mangling, a property that C++ has and C does not.

    I could be wrong but my guess is you are taking the wrong approach to
    learning; trying to absorb and have everything make sense as you encounter
    it. A great many people on these newsgroups implicitly advocate that but I,
    personally, have had very poor luck my entire life learning *anything* that
    way. Get fairly fluent in writing simple programs first, then go back and
    fill in the holes in your knowledge. What some people call a spiral or
    helical approach. It may take several revisits before you finally absorb it
    all - in the case of C++ you may very well die first.

    Looking at headers is not a good way for a novice to learn much of anything
    except that computer programming is a complex business.
     
    osmium, Jun 9, 2004
    #3
  4. [flame-shield: warning, partially OT]

    __cplusplus is a macro that a C implementation is *not* allowed to
    define in standard conforming mode. It's conventionally defined by
    C++ compilers. The extern "C" part tells a C++ compiler, that the
    following code requires C calling conventions. (Please note: further
    questions about C++ should be directed to comp.lang.c++ to avoid
    irritations.)
    You definitely want to read _The C Programming Language_ by Kernighan
    and Ritchie, second edition (K&R2), accompanied by the comp.lang.c-FAQ
    (link in signature). BTW, the FAQ provides some more good book
    recommendations in section 18.10.

    Good luck!

    Regards
     
    Irrwahn Grausewitz, Jun 9, 2004
    #4
  5. Arturo

    Dan Pop Guest

    ALL you need to know in the context of the C language is that a C compiler
    is NOT allowed to predefine the __cplusplus macro. Therefore, any time
    you see a conditional block starting with #ifdef __cplusplus you can
    *safely* ignore it.

    If you ever learn C++, such things will make sense when included in a C++
    program, but in C programs you can simply treat them as meaningless
    comments.

    Dan
     
    Dan Pop, Jun 9, 2004
    #5
  6. Not necessarily. The headers could be designed to be used by a C
    compiler and by a separate C++ compiler, both of which look for header
    files in the same place. (But since system headers tend to contain a
    lot of implementation-specific code, it's likely that the two
    compilers are closely related, even if they aren't a single program.)
     
    Keith Thompson, Jun 9, 2004
    #6
  7. Arturo

    Jack Klein Guest

    Close, but no cigar. C99 implementations are not allowed to define
    __cplusplus. Earlier versions of the standard had no such
    prohibitions.
     
    Jack Klein, Jun 10, 2004
    #7
  8. Arturo

    Jack Klein Guest

    This is true for compilers conforming to the C99 standard that you so
    love to malign. It is quite false for C90.
     
    Jack Klein, Jun 10, 2004
    #8
  9. Arturo

    Neil Kurzman Guest

    This specific case tells a C++ compiler that the functions for this
    library are C functions.
    This compiler calls C and C++ functions differently and needs to be
    informed that you are trying to call C functions from C++.
    But since this is the C group, It is just trivia.
     
    Neil Kurzman, Jun 10, 2004
    #9
  10. Arturo

    Dan Pop Guest

    For implementations of earlier versions of the standard, it was a (huge)
    quality of implementation issues.

    Dan
     
    Dan Pop, Jun 10, 2004
    #10
  11. Arturo

    Dan Pop Guest

    It is quite true for C90 *implementations*, unless you can prove
    otherwise.

    Dan
     
    Dan Pop, Jun 10, 2004
    #11
  12. Arturo

    tien

    Joined:
    Sep 8, 2006
    Messages:
    1
    Likes Received:
    0
    I have an additional question about __cplusplus. For detail, I am using GNU g++. But maybe the question isn't GNU g++ specific.

    -In what g++ header that __cplusplus is defined? Or I have to define the macro myself in my c++ source (in order to have a proper __cplusplus defined to latterly use whenever needed)? Or it is automatically inserted by g++ at compiling?

    I have been working in a project in which C++ program links with other C libs.
    I have been using stuff:
    #ifdef __cplusplus
    extern "C" {
    #endif
    a long time, but didn't know or ask the above question myself until this confusing thing occurs:

    The code used to work this way:
    -The C headers always have #ifdef __cplusplus extern "C" { #endif stuff around function prototype declarations
    -The C++ sources, though already include the C headers, always duplicate the C function prototype declarations by declaring them again inside extern "C" { ...} (without #ifdef __cplusplus)

    Then, I thought that the duplications in C++ sources were redundant and removed them.
    First, compiling and linking were ok. But the program went wrong when running. Argument passing was wrong when calling a C funtion. At first, I think __cplusplus declaration was missed, so the extern "C" in C header didn't work. So the above question comes in my mind. Where __cplusplus is defined?

    But it seems that it isn't the only question. Next, I tried to also remove #ifdef __cplusplus extern "C" { #endif in C headers. Then the compiler complained about "undefined reference to function ..." while linking, which means, __cplusplus is actually always defined (so the compiler was satisfied in the previous test). But I still question where the macro is defined? And why we need both extern "C" in C header and a duplication of the same thing in C++ source?

    Thanks!
     
    tien, Sep 8, 2006
    #12
  13. Arturo

    barun

    Joined:
    May 9, 2008
    Messages:
    1
    Likes Received:
    0
    Location:
    Bangalore
    Try this program

    #include <stdio.h>

    int
    main(void) {
    #ifdef __cplusplus
    printf("C++\n");
    #endif
    printf("C\n");

    return 0;
    }

    Now compile this in two ways:

    gcc -g -Wall cplusplus.c -o c
    g++ -g -Wall cplusplus.c -o cpp

    And then run the both:
    ./c
    ./cpp

    You will see that __cplusplus is known to g++ but not to gcc

    This is not an answer to your question, but may be a nice example.
     
    barun, May 9, 2008
    #13
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.