Re: #ifdef __cplusplus

Discussion in 'C Programming' started by Philip Potter, Apr 9, 2008.

  1. mattia wrote:
    > I've see in some code:
    >
    > #ifdef __cplusplus
    > extern "C"
    > {
    > #endif
    >
    > what does it mean?
    >
    > Thanks


    Others have stated what it means; I'd just add that I've usually seen
    this idiom used in system header files (such as stdio.h) so that the
    same header can be used for both C and C++ compilers. C compilers (DS9K
    excluded) don't define __cplusplus and don't see the extern "C" bit,
    while C++ compilers do define it and so know that all the stdio.h
    functions declared are C-style functions and linked in a C-style way.

    I wouldn't recommend this practice in your own code.

    extern "C" has no defined meaning in C. You'd probably get better
    answers in comp.lang.c++.

    Philip
     
    Philip Potter, Apr 9, 2008
    #1
    1. Advertising

  2. Philip Potter

    Richard Guest

    Philip Potter <> writes:

    > mattia wrote:
    >> I've see in some code:
    >>
    >> #ifdef __cplusplus
    >> extern "C"
    >> {
    >> #endif
    >>
    >> what does it mean?
    >>
    >> Thanks

    >
    > Others have stated what it means; I'd just add that I've usually seen
    > this idiom used in system header files (such as stdio.h) so that the
    > same header can be used for both C and C++ compilers. C compilers (DS9K
    > excluded) don't define __cplusplus and don't see the extern "C" bit,
    > while C++ compilers do define it and so know that all the stdio.h
    > functions declared are C-style functions and linked in a C-style way.
    >
    > I wouldn't recommend this practice in your own code.
    >
    > extern "C" has no defined meaning in C. You'd probably get better
    > answers in comp.lang.c++.
    >
    > Philip


    The answer has been given exactly as is more than once. Traipsing over
    there is slightly overkill IMO.
     
    Richard, Apr 9, 2008
    #2
    1. Advertising

  3. Richard wrote:
    > Philip Potter <> writes:
    >
    >> mattia wrote:
    >>> I've see in some code:
    >>>
    >>> #ifdef __cplusplus
    >>> extern "C"
    >>> {
    >>> #endif
    >>>
    >>> what does it mean?
    >>>
    >>> Thanks

    >> Others have stated what it means; I'd just add that I've usually seen
    >> this idiom used in system header files (such as stdio.h) so that the
    >> same header can be used for both C and C++ compilers. C compilers (DS9K
    >> excluded) don't define __cplusplus and don't see the extern "C" bit,
    >> while C++ compilers do define it and so know that all the stdio.h
    >> functions declared are C-style functions and linked in a C-style way.
    >>
    >> I wouldn't recommend this practice in your own code.
    >>
    >> extern "C" has no defined meaning in C. You'd probably get better
    >> answers in comp.lang.c++.
    >>
    >> Philip

    >
    > The answer has been given exactly as is more than once. Traipsing over
    > there is slightly overkill IMO.


    It's up to the OP. __cplusplus doesn't have much meaning in C, and
    extern "C" even less, so if you want to be sure of your answers, you're
    better asking a C++ expert.

    Phil
     
    Philip Potter, Apr 9, 2008
    #3
  4. Philip Potter

    Richard Guest

    Philip Potter <> writes:

    > Richard wrote:
    >> Philip Potter <> writes:
    >>
    >>> mattia wrote:
    >>>> I've see in some code:
    >>>>
    >>>> #ifdef __cplusplus
    >>>> extern "C"
    >>>> {
    >>>> #endif
    >>>>
    >>>> what does it mean?
    >>>>
    >>>> Thanks
    >>> Others have stated what it means; I'd just add that I've usually seen
    >>> this idiom used in system header files (such as stdio.h) so that the
    >>> same header can be used for both C and C++ compilers. C compilers (DS9K
    >>> excluded) don't define __cplusplus and don't see the extern "C" bit,
    >>> while C++ compilers do define it and so know that all the stdio.h
    >>> functions declared are C-style functions and linked in a C-style way.
    >>>
    >>> I wouldn't recommend this practice in your own code.
    >>>
    >>> extern "C" has no defined meaning in C. You'd probably get better
    >>> answers in comp.lang.c++.
    >>>
    >>> Philip

    >>
    >> The answer has been given exactly as is more than once. Traipsing over
    >> there is slightly overkill IMO.

    >
    > It's up to the OP. __cplusplus doesn't have much meaning in C, and
    > extern "C" even less, so if you want to be sure of your answers, you're
    > better asking a C++ expert.
    >
    > Phil


    As I said - the answer was already posted many times. There are many
    here who know both - and it is a "both" question since many C
    programmers mix and match.
     
    Richard, Apr 9, 2008
    #4
  5. Philip Potter

    Ian Collins Guest

    mattia wrote:
    >
    > I think that I'll note use __cplusplus unless I'll have to link my
    > headers with cpp code. That will be my rule. Thanks all for the reply.
    >

    If you think there's the remotest chance your C library might be used
    with C++, you may as well use the "extern C" wrappers. There's no
    cost/impact to C code and you might save you or your users future work.

    I always use "extern C" wrappers.

    --
    Ian Collins.
     
    Ian Collins, Apr 9, 2008
    #5
    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,405
    barun
    May 9, 2008
  2. Walter Roberson

    Re: #ifdef __cplusplus

    Walter Roberson, Apr 9, 2008, in forum: C Programming
    Replies:
    8
    Views:
    1,747
    Keith Thompson
    Apr 10, 2008
  3. Antoninus Twink

    Re: #ifdef __cplusplus

    Antoninus Twink, Apr 9, 2008, in forum: C Programming
    Replies:
    5
    Views:
    510
    Keith Thompson
    Apr 9, 2008
  4. Flash Gordon

    Re: #ifdef __cplusplus

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

    Re: #ifdef __cplusplus

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

Share This Page