Re: #ifdef __cplusplus

Discussion in 'C Programming' started by Walter Roberson, Apr 9, 2008.

  1. In article <47fcee3f$0$41659$>,
    mattia <> wrote:
    >I've see in some code:


    >#ifdef __cplusplus
    > extern "C"
    > {
    >#endif
    >
    >what does it mean?


    This has to do with C++. A C compiler will not defne __cplusplus
    and so a C compiler will compile those lines to nothingness.

    This form detects that whether the code is being compiled with a C++
    compiler, and if it is, wraps extern "C" { } around the block of code.
    The meaning of extern "C" { } is specific to C++, and in C++ means
    that the enclosed block of code is to have its functions compiled
    with C calling sequences rather than C++ calling sequences. The
    single most important difference between C calling sequences and C++
    calling sequences is that C++ calling sequences (usually) have
    a hidden parameter named "this" that refers to the object being
    operated upon; using this syntax in C++ tells C++ to not put in
    that hidden parameter, thus leaving the functions defined suitable for
    calling from C (or, alternately, -declaring- functions as being
    external functions that need C-style parameters to call.)
    --
    "When we all think alike no one is thinking very much."
    -- Walter Lippmann
     
    Walter Roberson, Apr 9, 2008
    #1
    1. Advertising

  2. Walter Roberson wrote:
    > In article <47fcee3f$0$41659$>,
    > mattia <> wrote:
    >> I've see in some code:

    >
    >> #ifdef __cplusplus
    >> extern "C"
    >> {
    >> #endif
    >>
    >> what does it mean?

    >
    > This has to do with C++. A C compiler will not defne __cplusplus
    > and so a C compiler will compile those lines to nothingness.


    My DS9K defines __cplusplus.

    (By which I mean, there's nothing in the C Standard preventing a
    conforming implementation defining __cplusplus, though it would be a
    Dumb Thing for a C implementation to do, and I'd be happy relying on my
    implementation not to define it.)

    > This form detects that whether the code is being compiled with a C++
    > compiler, and if it is, wraps extern "C" { } around the block of code.
    > The meaning of extern "C" { } is specific to C++, and in C++ means
    > that the enclosed block of code is to have its functions compiled
    > with C calling sequences rather than C++ calling sequences. The
    > single most important difference between C calling sequences and C++
    > calling sequences is that C++ calling sequences (usually) have
    > a hidden parameter named "this" that refers to the object being
    > operated upon; using this syntax in C++ tells C++ to not put in
    > that hidden parameter, thus leaving the functions defined suitable for
    > calling from C (or, alternately, -declaring- functions as being
    > external functions that need C-style parameters to call.)


    Although you are correct about the implicit 'this', it only applies to
    class methods, which I don't think you'd want to put into an extern "C"
    block. I believe a more important factor is that C++ needs to "mangle"
    function names to allow function overloading, and since C doesn't know
    the magic of C++ mangling (which is different on different C++
    compilers), C++ must be told to turn off such features.
     
    Philip Potter, Apr 9, 2008
    #2
    1. Advertising

  3. Philip Potter <> writes:

    > Walter Roberson wrote:
    >> In article <47fcee3f$0$41659$>,
    >> mattia <> wrote:
    >>> I've see in some code:

    >>
    >>> #ifdef __cplusplus
    >>> extern "C"
    >>> {
    >>> #endif
    >>>
    >>> what does it mean?

    >>
    >> This has to do with C++. A C compiler will not defne __cplusplus
    >> and so a C compiler will compile those lines to nothingness.

    >
    > My DS9K defines __cplusplus.
    >
    > (By which I mean, there's nothing in the C Standard preventing a
    > conforming implementation defining __cplusplus, though it would be a
    > Dumb Thing for a C implementation to do, and I'd be happy relying on my
    > implementation not to define it.)


    In section 6.10.8 (Predefined macro names) paragraph 5 read:

    The implementation shall not predefine the macro __cplusplus, nor
    shall it define it in any standard header.

    --
    Ben.
     
    Ben Bacarisse, Apr 9, 2008
    #3
  4. Ben Bacarisse wrote:
    > Philip Potter <> writes:
    >> My DS9K defines __cplusplus.
    >>
    >> (By which I mean, there's nothing in the C Standard preventing a
    >> conforming implementation defining __cplusplus, though it would be a
    >> Dumb Thing for a C implementation to do, and I'd be happy relying on my
    >> implementation not to define it.)

    >
    > In section 6.10.8 (Predefined macro names) paragraph 5 read:
    >
    > The implementation shall not predefine the macro __cplusplus, nor
    > shall it define it in any standard header.
    >

    Well I never! I stand completely and utterly corrected. Is that in C89 too?
     
    Philip Potter, Apr 9, 2008
    #4
  5. Walter Roberson

    Flash Gordon Guest

    Philip Potter wrote, On 09/04/08 18:39:
    > Walter Roberson wrote:


    <snip>

    >> This has to do with C++. A C compiler will not defne __cplusplus
    >> and so a C compiler will compile those lines to nothingness.

    >
    > My DS9K defines __cplusplus.


    In which case you should upgrade to the C99 compiler for the DS9K.

    > (By which I mean, there's nothing in the C Standard preventing a
    > conforming implementation defining __cplusplus, though it would be a
    > Dumb Thing for a C implementation to do, and I'd be happy relying on my
    > implementation not to define it.)


    <snip>

    C99 banned implementations from defining __cplusplus
    --
    Flash Gordon
     
    Flash Gordon, Apr 9, 2008
    #5
  6. Walter Roberson

    Richard Guest

    Philip Potter <> writes:

    > Walter Roberson wrote:
    >> In article <47fcee3f$0$41659$>,
    >> mattia <> wrote:
    >>> I've see in some code:

    >>
    >>> #ifdef __cplusplus
    >>> extern "C"
    >>> {
    >>> #endif
    >>>
    >>> what does it mean?

    >>
    >> This has to do with C++. A C compiler will not defne __cplusplus
    >> and so a C compiler will compile those lines to nothingness.

    >
    > My DS9K defines __cplusplus.
    >
    > (By which I mean, there's nothing in the C Standard preventing a
    > conforming implementation defining __cplusplus, though it would be a
    > Dumb Thing for a C implementation to do, and I'd be happy relying on my
    > implementation not to define it.)


    Nonsense. That would make a mockery of the whole thing.
     
    Richard, Apr 9, 2008
    #6
  7. Philip Potter <> writes:
    > Ben Bacarisse wrote:
    >> Philip Potter <> writes:
    >>> My DS9K defines __cplusplus.
    >>>
    >>> (By which I mean, there's nothing in the C Standard preventing a
    >>> conforming implementation defining __cplusplus, though it would be a
    >>> Dumb Thing for a C implementation to do, and I'd be happy relying on my
    >>> implementation not to define it.)

    >>
    >> In section 6.10.8 (Predefined macro names) paragraph 5 read:
    >>
    >> The implementation shall not predefine the macro __cplusplus, nor
    >> shall it define it in any standard header.
    >>

    > Well I never! I stand completely and utterly corrected. Is that in C89 too?


    I don't think so.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Apr 9, 2008
    #7
  8. Walter Roberson

    Default User Guest

    Walter Roberson wrote:

    > In article <47fcee3f$0$41659$>,
    > mattia <> wrote:
    > > I've see in some code:

    >
    > > #ifdef __cplusplus
    > > extern "C"
    > > {
    > > #endif
    > >
    > > what does it mean?

    >
    > This has to do with C++. A C compiler will not defne __cplusplus
    > and so a C compiler will compile those lines to nothingness.
    >
    > This form detects that whether the code is being compiled with a C++
    > compiler, and if it is, wraps extern "C" { } around the block of
    > code. The meaning of extern "C" { } is specific to C++, and in C++
    > means that the enclosed block of code is to have its functions
    > compiled with C calling sequences rather than C++ calling sequences.
    > The single most important difference between C calling sequences and
    > C++ calling sequences is that C++ calling sequences (usually) have
    > a hidden parameter named "this" that refers to the object being
    > operated upon; using this syntax in C++ tells C++ to not put in
    > that hidden parameter, thus leaving the functions defined suitable for
    > calling from C (or, alternately, -declaring- functions as being
    > external functions that need C-style parameters to call.)


    What? No. extern "C" is a linkage specification. It usually means that
    it won't use name-mangling, but the specifics aren't spelled out in the
    C++ standard. What it says (in part):

    Every implementation shall provide for linkage to functions written
    in the C programming language, "C", and linkage to C++ functions,
    "C++". [Example:
    complex sqrt(complex); // C + + linkage by default
    extern "C" {
    double sqrt(double); // C linkage
    }
    -end example]



    Brian
     
    Default User, Apr 9, 2008
    #8
  9. Richard Heathfield <> writes:
    > Keith Thompson said:
    >> Philip Potter <> writes:
    >>> Ben Bacarisse wrote:
    >>>> Philip Potter <> writes:
    >>>>> My DS9K defines __cplusplus.
    >>>>>
    >>>>> (By which I mean, there's nothing in the C Standard preventing a
    >>>>> conforming implementation defining __cplusplus, though it would be a
    >>>>> Dumb Thing for a C implementation to do, and I'd be happy relying on
    >>>>> my implementation not to define it.)
    >>>>
    >>>> In section 6.10.8 (Predefined macro names) paragraph 5 read:
    >>>>
    >>>> The implementation shall not predefine the macro __cplusplus, nor
    >>>> shall it define it in any standard header.
    >>>>
    >>> Well I never! I stand completely and utterly corrected. Is that in C89
    >>> too?

    >>
    >> I don't think so.

    >
    > It isn't in the C89 draft. If nobody can produce a C90 cite, I think it's
    > reasonable to assume it's not in the C90 standard. Which means the de
    > facto standard has nothing to say on the matter, and Philip Potter's
    > point, albeit somewhat pharisaical, is nevertheless correct.
    >
    > It /is/ in the C99 draft, which means that any conforming C99
    > implementation that should one day happen to come along will *not* be
    > allowed to define __cplusplus, in which case there seems to me to be no
    > point in testing for it in a preprocessor directive.


    C90 does not mention __cplusplus.

    However, I'm willing to assume that no conforming C90 compiler will
    ever predefine __cplusplus, and to rely on that assumption in my code
    if it's convenient to do so. And if I ever see a C compiler that does
    so, I'll consider it a bug (even if it's not strictly a conformance
    issue for the standard to which it purports to conform).

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Apr 10, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arturo

    Help with __cplusplus

    Arturo, Jun 9, 2004, in forum: C Programming
    Replies:
    12
    Views:
    30,374
    barun
    May 9, 2008
  2. Antoninus Twink

    Re: #ifdef __cplusplus

    Antoninus Twink, Apr 9, 2008, in forum: C Programming
    Replies:
    5
    Views:
    501
    Keith Thompson
    Apr 9, 2008
  3. Philip Potter

    Re: #ifdef __cplusplus

    Philip Potter, Apr 9, 2008, in forum: C Programming
    Replies:
    4
    Views:
    566
    Ian Collins
    Apr 9, 2008
  4. Flash Gordon

    Re: #ifdef __cplusplus

    Flash Gordon, Apr 9, 2008, in forum: C Programming
    Replies:
    10
    Views:
    657
    Flash Gordon
    Apr 11, 2008
  5. Ian Collins

    Re: #ifdef __cplusplus

    Ian Collins, Apr 9, 2008, in forum: C Programming
    Replies:
    11
    Views:
    676
Loading...

Share This Page