#ifdef __cplusplus

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip Potter

    #16
    Re: #ifdef __cplusplus

    Richard wrote:
    Philip Potter <pgp@doc.ic.ac. ukwrites:
    >
    >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

    Comment

    • Richard

      #17
      Re: #ifdef __cplusplus

      Philip Potter <pgp@doc.ic.ac. ukwrites:
      Richard wrote:
      >Philip Potter <pgp@doc.ic.ac. ukwrites:
      >>
      >>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.

      Comment

      • Ian Collins

        #18
        Re: #ifdef __cplusplus

        Richard Heathfield wrote:
        mattia said:
        >
        >I've see in some code:
        >>
        >#ifdef __cplusplus
        > extern "C"
        > {
        >#endif
        >>
        >what does it mean?
        >
        It means that someone hasn't decided which language they're writing in.
        Utter nonsense. So are you saying the authours of all my system headers
        didn't know which language they're writing in?

        --
        Ian Collins.

        Comment

        • Keith Thompson

          #19
          Re: #ifdef __cplusplus

          Philip Potter <pgp@doc.ic.ac. ukwrites:
          Ben Bacarisse wrote:
          >Philip Potter <pgp@doc.ic.ac. ukwrites:
          >>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
          >>implementatio n 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_Keit h) <kst-u@mib.org>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • mattia

            #20
            Re: #ifdef __cplusplus

            On Wed, 09 Apr 2008 21:34:06 +0200, Richard wrote:
            Philip Potter <pgp@doc.ic.ac. ukwrites:
            >
            >Richard wrote:
            >>Philip Potter <pgp@doc.ic.ac. ukwrites:
            >>>
            >>>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.
            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.


            --
            __mattia__

            Comment

            • Keith Thompson

              #21
              Re: #ifdef __cplusplus

              Richard <devr_@gmail.co mwrites:
              Antoninus Twink <nospam@nospam. invalidwrites:
              >On 9 Apr 2008 at 16:26, mattia wrote:
              >>I've see in some code:
              >>>
              >>#ifdef __cplusplus
              >> extern "C"
              >> {
              >>#endif
              >>>
              >>what does it mean?
              >>
              [off-topic explanation deleted]
              >
              Very well explained. I'm amazed no one has scolded you and told the OP
              that the "experts" are down the corridoor second on the left ....
              Would it do any good? AT is apparently determined to ignore the fact
              that this newsgroup is for discussion of the C programming language.
              Of course most of the regulars killfiled him long ago. They might
              want to consider unkillfiling him, at least for a while, to see what
              he's up to now.

              Some of what he's written recently might be valuable *if* it were
              posted in an appropriate forum.

              Meanwhile, you and your friends continue to make no actual
              contribution, preferring to spend your time whining about other
              posters.

              --
              Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Ian Collins

                #22
                Re: #ifdef __cplusplus

                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.

                Comment

                • Default User

                  #23
                  Re: #ifdef __cplusplus

                  Walter Roberson wrote:
                  In article <47fcee3f$0$416 59$4fafbaef@rea der4.news.tin.i t>,
                  mattia <gervaz@gmail.c omwrote:
                  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

                  Comment

                  • Richard Heathfield

                    #24
                    Re: #ifdef __cplusplus

                    Flash Gordon said:
                    Richard Heathfield wrote, On 09/04/08 17:41:
                    >mattia said:
                    >>
                    >>I've see in some code:
                    >>>
                    >>#ifdef __cplusplus
                    >> extern "C"
                    >> {
                    >>#endif
                    >>>
                    >>what does it mean?
                    >>
                    >It means that someone hasn't decided which language they're writing in.
                    >
                    No it does not.
                    Yes, it does.
                    It is generally used in the header when the library is
                    written in C but might be called from C++.
                    See?

                    Anyway, I know this view won't be a popular one but, in my view, mixing C
                    and C++ like this in a translation unit that is intended to be seen by a C
                    compiler is a mistake.
                    >Best avoided.
                    >
                    No, best used when it is appropriate.
                    On the contrary - best avoided. There is an alternative, trivial, technique
                    that does not involve altering the C source to suit C++, yet which suits
                    C++ perfectly.
                    For more discussion about this the correct place would be comp.lang.c++
                    It depends on whether you want to talk about C++ (which I don't), or about
                    the advisability or otherwise of introducing illegal code into a C
                    translation unit, protected only by preprocessor directives. I fail to see
                    why that's topical in comp.lang.c++, given that it's a question about a C
                    translation unit.

                    --
                    Richard Heathfield <http://www.cpax.org.uk >
                    Email: -http://www. +rjh@
                    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    "Usenet is a strange place" - dmr 29 July 1999

                    Comment

                    • Richard Heathfield

                      #25
                      Re: #ifdef __cplusplus

                      Ian Collins said:
                      Richard Heathfield wrote:
                      >mattia said:
                      >>
                      >>I've see in some code:
                      >>>
                      >>#ifdef __cplusplus
                      >> extern "C"
                      >> {
                      >>#endif
                      >>>
                      >>what does it mean?
                      >>
                      >It means that someone hasn't decided which language they're writing in.
                      >
                      Utter nonsense.
                      Hmmm.
                      So are you saying the authours of all my system headers
                      didn't know which language they're writing in?
                      Are /you/ saying they've introduced illegal syntax into C translation
                      units, protected only by a #ifdef of a symbol for which the Standard
                      defines no meaning and on which C90 imposes no special conditions?

                      If so, then yes, I'd say they didn't know which language they were writing
                      in.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Dann Corbit

                        #26
                        Re: #ifdef __cplusplus

                        "Richard Heathfield" <rjh@see.sig.in validwrote in message
                        news:dtedndeFxM jAq2DanZ2dneKdn ZydnZ2d@bt.com. ..
                        Flash Gordon said:
                        >
                        >Richard Heathfield wrote, On 09/04/08 17:41:
                        >>mattia said:
                        >>>
                        >>>I've see in some code:
                        >>>>
                        >>>#ifdef __cplusplus
                        >>> extern "C"
                        >>> {
                        >>>#endif
                        >>>>
                        >>>what does it mean?
                        >>>
                        >>It means that someone hasn't decided which language they're writing in.
                        >>
                        >No it does not.
                        >
                        Yes, it does.
                        >
                        >It is generally used in the header when the library is
                        >written in C but might be called from C++.
                        >
                        See?
                        >
                        Anyway, I know this view won't be a popular one but, in my view, mixing C
                        and C++ like this in a translation unit that is intended to be seen by a C
                        compiler is a mistake.
                        >
                        >>Best avoided.
                        >>
                        >No, best used when it is appropriate.
                        >
                        On the contrary - best avoided. There is an alternative, trivial,
                        technique
                        that does not involve altering the C source to suit C++, yet which suits
                        C++ perfectly.
                        >
                        >For more discussion about this the correct place would be comp.lang.c++
                        >
                        It depends on whether you want to talk about C++ (which I don't), or about
                        the advisability or otherwise of introducing illegal code into a C
                        translation unit, protected only by preprocessor directives. I fail to see
                        why that's topical in comp.lang.c++, given that it's a question about a C
                        translation unit.
                        From ISO/IEC 9899:1999 (E) Section 6.10.9 (Language) page 161 we have this:
                        "5 The implementation shall not predefine the macro _ _cplusplus, nor shall
                        it define it in any standard header."




                        ** Posted from http://www.teranews.com **

                        Comment

                        • Ian Collins

                          #27
                          Re: #ifdef __cplusplus

                          Richard Heathfield wrote:
                          Ian Collins said:
                          >
                          >Richard Heathfield wrote:
                          >>mattia said:
                          >>>
                          >>>I've see in some code:
                          >>>>
                          >>>#ifdef __cplusplus
                          >>> extern "C"
                          >>> {
                          >>>#endif
                          >>>>
                          >>>what does it mean?
                          >>It means that someone hasn't decided which language they're writing in.
                          >Utter nonsense.
                          >
                          Hmmm.
                          >
                          >So are you saying the authours of all my system headers
                          >didn't know which language they're writing in?
                          >
                          Are /you/ saying they've introduced illegal syntax into C translation
                          units, protected only by a #ifdef of a symbol for which the Standard
                          defines no meaning and on which C90 imposes no special conditions?
                          >
                          In all but one header, no. They are simply informing C++ compilers that
                          the C standard library function have C linkage. No illegal syntax there.

                          The exception is in <string.h>, where C++ adds overloads for the broken
                          C library functions (strchr() for example) that return a char* to a
                          const char* argument.
                          If so, then yes, I'd say they didn't know which language they were writing
                          in.
                          >
                          If so, they most certainly did.

                          --
                          Ian Collins.

                          Comment

                          • Richard Heathfield

                            #28
                            Re: #ifdef __cplusplus

                            Ian Collins said:
                            Richard Heathfield wrote:
                            >Ian Collins said:
                            >>
                            <snip>
                            >>
                            >>So are you saying the authours of all my system headers
                            >>didn't know which language they're writing in?
                            >>
                            >Are /you/ saying they've introduced illegal syntax into C translation
                            >units, protected only by a #ifdef of a symbol for which the Standard
                            >defines no meaning and on which C90 imposes no special conditions?
                            >>
                            In all but one header, no. They are simply informing C++ compilers
                            This is comp.lang.c, where we discuss C, not C++. Do /you/ know what
                            language you're writing in? :-)
                            that
                            the C standard library function have C linkage. No illegal syntax there.
                            The syntax

                            extern "C"

                            is illegal *C*. If you want to discuss whether it's legal C++, I suggest
                            you raise the matter in comp.lang.c++ - but here in comp.lang.c, it's
                            illegal. In C, extern takes a declaration, not a string literal.

                            <snip>

                            --
                            Richard Heathfield <http://www.cpax.org.uk >
                            Email: -http://www. +rjh@
                            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                            "Usenet is a strange place" - dmr 29 July 1999

                            Comment

                            • Ian Collins

                              #29
                              Re: #ifdef __cplusplus

                              Richard Heathfield wrote:
                              Dann Corbit said:
                              >
                              >"Richard Heathfield" <rjh@see.sig.in validwrote in message
                              >news:dtedndeFx MjAq2DanZ2dneKd nZydnZ2d@bt.com ...
                              >>Flash Gordon said:
                              >>>
                              <snip>
                              >>>For more discussion about this the correct place would be comp.lang.c++
                              >>It depends on whether you want to talk about C++ (which I don't), or
                              >>about the advisability or otherwise of introducing illegal code into a C
                              >>translation unit, protected only by preprocessor directives. I fail to
                              >>see why that's topical in comp.lang.c++, given that it's a question
                              >>about a C translation unit.
                              >From ISO/IEC 9899:1999 (E) Section 6.10.9 (Language) page 161 we have
                              >this: "5 The implementation shall not predefine the macro _ _cplusplus,
                              >nor shall it define it in any standard header."
                              >
                              Right. And it's in a reserved namespace, too. So why bother testing for it?
                              It's in the implementation' s namespace, so the the "implementation " is
                              free to define it. The "implementation " might not be a C compiler.

                              --
                              Ian Collins.

                              Comment

                              • Richard Heathfield

                                #30
                                Re: #ifdef __cplusplus

                                Ian Collins said:

                                <snip>
                                [__cplusplus is] in the implementation' s namespace, so the the
                                "implementation " is free to define it.
                                Not if it's a C99 implementation. And a C90 implementation is free to
                                define it to mean anything it likes.
                                The "implementation " might not be a C compiler.
                                In which case it's hardly relevant in comp.lang.c (unless, of course, it's
                                a C interpreter).

                                --
                                Richard Heathfield <http://www.cpax.org.uk >
                                Email: -http://www. +rjh@
                                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                                "Usenet is a strange place" - dmr 29 July 1999

                                Comment

                                Working...