#ifdef __cplusplus

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

    #ifdef __cplusplus

    I've see in some code:

    #ifdef __cplusplus
    extern "C"
    {
    #endif

    what does it mean?

    Thanks

    --
    __mattia__
  • Richard Heathfield

    #2
    Re: #ifdef __cplusplus

    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.
    Best avoided.

    --
    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

    • Walter Roberson

      #3
      Re: #ifdef __cplusplus

      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.)
      --
      "When we all think alike no one is thinking very much."
      -- Walter Lippmann

      Comment

      • Kaz Kylheku

        #4
        Re: #ifdef __cplusplus

        On Apr 9, 9:26 am, mattia <ger...@gmail.c omwrote:
        I've see in some code:
        >
        #ifdef __cplusplus
         extern "C"
         {
        #endif
        >
        what does it mean?
        It means that the tokens extern "C" { are part of the
        translation unit if the __cplusplus preprocessor symbol is defined.

        Somewhere farther down, there should be a matching

        #ifdef __cplusplus
        }
        #endif

        to close the brace.

        Comment

        • Antoninus Twink

          #5
          Re: #ifdef __cplusplus

          On 9 Apr 2008 at 16:26, mattia wrote:
          I've see in some code:
          >
          #ifdef __cplusplus
          extern "C"
          {
          #endif
          >
          what does it mean?
          C++ supports certain powerful but complex features that mean that
          different functions and variables can have different names:
          specifically, in C++ function names can be overloaded (two functions can
          have the same name, but different argument signatures), and also C++
          supports "namespaces ", which granulate visibility in C's single global
          external namespace.

          To get around these problems, C++ compilers commonly "mangle" function
          names: that is, the function name that appears in the global namespace
          will be a string obtained in some way or other from the actual name of
          the function as it appears in the code, the type and number of its
          arguments, and any namespace qualifications needed to specify it.

          This means that if a C++ program tries to use functions from a C
          library, then there will be problems: when the C++ linker sees a
          function called foobar, it will actually look in the symbol table for
          something like foorbar__Fii.

          To get around this, there's a way for the programmer to tell his
          C++ linker that some of the functions have "C linkage", and so it
          shouldn't try to mangle their names. This is achived by surrounding the
          code with
          extern "C" {
          ....
          }

          Sometimes, people write functions in C and then want to create a single
          header that will let people use their functions either in C or C++.

          To accomplish this, they make use of the fact that the macro __cplusplus
          will be defined by the compiler if and only if it's compiling the
          program as C++, and include the extern "C" lines conditional on the
          presence of this macro. Therefore, if the file is being compiled as C
          then this little bit of C++ will vanish during preprocessing, and won't
          choke the C compiler.

          Comment

          • Philip Potter

            #6
            Re: #ifdef __cplusplus

            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

            Comment

            • Richard

              #7
              Re: #ifdef __cplusplus

              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?
              >
              C++ supports certain powerful but complex features that mean that
              different functions and variables can have different names:
              specifically, in C++ function names can be overloaded (two functions can
              have the same name, but different argument signatures), and also C++
              supports "namespaces ", which granulate visibility in C's single global
              external namespace.
              >
              To get around these problems, C++ compilers commonly "mangle" function
              names: that is, the function name that appears in the global namespace
              will be a string obtained in some way or other from the actual name of
              the function as it appears in the code, the type and number of its
              arguments, and any namespace qualifications needed to specify it.
              >
              This means that if a C++ program tries to use functions from a C
              library, then there will be problems: when the C++ linker sees a
              function called foobar, it will actually look in the symbol table for
              something like foorbar__Fii.
              >
              To get around this, there's a way for the programmer to tell his
              C++ linker that some of the functions have "C linkage", and so it
              shouldn't try to mangle their names. This is achived by surrounding the
              code with
              extern "C" {
              ...
              }
              >
              Sometimes, people write functions in C and then want to create a single
              header that will let people use their functions either in C or C++.
              >
              To accomplish this, they make use of the fact that the macro __cplusplus
              will be defined by the compiler if and only if it's compiling the
              program as C++, and include the extern "C" lines conditional on the
              presence of this macro. Therefore, if the file is being compiled as C
              then this little bit of C++ will vanish during preprocessing, and won't
              choke the C compiler.
              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 ....

              Comment

              • Antoninus Twink

                #8
                Re: #ifdef __cplusplus

                On 9 Apr 2008 at 17:48, Richard wrote:
                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 ....
                Thanks.

                Luckily they've all kill-filed me, so I can safely post useful
                information without generating a dozen flames in response.

                Comment

                • Ben Bacarisse

                  #9
                  Re: #ifdef __cplusplus

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

                  Comment

                  • Philip Potter

                    #10
                    Re: #ifdef __cplusplus

                    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
                    >implementati on 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?

                    Comment

                    • Kenny McCormack

                      #11
                      Re: #ifdef __cplusplus

                      In article <slrnfvq0v8.479 .nospam@nospam. invalid>,
                      Antoninus Twink <nospam@nospam. invalidwrote:
                      >On 9 Apr 2008 at 17:48, Richard wrote:
                      >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 ....
                      >
                      >Thanks.
                      >
                      >Luckily they've all kill-filed me, so I can safely post useful
                      >information without generating a dozen flames in response.
                      >
                      It sounds like you have actually solved the problem of CLC.

                      Congratulations .

                      Essentially, there will be two CLCs - the one populated and run by the
                      regs (which, as we know, is 65% endless topicality BS, 30% beginner
                      questions and answers[*], and 5% [boring] lawyering) - and the other
                      one (unseen, as you say, by the regs) populated by the rest of us.
                      [*] By the way, tell me again, how _do_ you prototype main()?

                      Comment

                      • Flash Gordon

                        #12
                        Re: #ifdef __cplusplus

                        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

                        Comment

                        • Flash Gordon

                          #13
                          Re: #ifdef __cplusplus

                          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. It is generally used in the header when the library is
                          written in C but might be called from C++.
                          Best avoided.
                          No, best used when it is appropriate.

                          For more discussion about this the correct place would be comp.lang.c++
                          --
                          Flash Gordon

                          Comment

                          • Richard

                            #14
                            Re: #ifdef __cplusplus

                            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.

                            Comment

                            • Richard

                              #15
                              Re: #ifdef __cplusplus

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

                              Comment

                              Working...