extern "c" usage?!!

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

    extern "c" usage?!!

    What's that preprocessor do
    #ifdef __cplusplus
    extern "C" {
    #endif
    ..
    ..
    ..
    #ifdef __cplusplus
    }
    #endif

    and how you say extern "C" , i mean how u extern constant!!
  • Mike Wahler

    #2
    Re: extern "c&quot ; usage?!!


    "Medvedev" <3D.v.World@gma il.comwrote in message
    news:7fcf3a56-f5a7-457d-a87b-60429656b943@t5 4g2000hsg.googl egroups.com...
    What's that preprocessor do
    #ifdef __cplusplus
    extern "C" {
    #endif
    .
    .
    .
    #ifdef __cplusplus
    }
    #endif
    >
    and how you say extern "C" , i mean how u extern constant!!
    extern "C"
    {

    }

    ... causes any functions inside the braces to have
    "C linkage", so that they can be called from C
    functions. There's nothing about 'constant' here.

    The #ifdef will cause 'extern C' to be applied only
    if translating C++ code.

    -Mike


    Comment

    • Medvedev

      #3
      Re: extern &quot;c&quot ; usage?!!

      On Jul 6, 2:12 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
      "Medvedev" <3D.v.Wo...@gma il.comwrote in message
      >
      news:7fcf3a56-f5a7-457d-a87b-60429656b943@t5 4g2000hsg.googl egroups.com...
      >
      What's that preprocessor do
      #ifdef __cplusplus
      extern "C" {
      #endif
      .
      .
      .
      #ifdef __cplusplus
      }
      #endif
      >
      and how you say extern "C" , i mean how u extern constant!!
      >
      extern "C"
      {
      >
      }
      >
      .. causes any functions inside the braces to have
      "C linkage", so that they can be called from C
      functions. There's nothing about 'constant' here.
      sorry , but what differ C++ functions from C ones

      Comment

      • Jim Langston

        #4
        Re: extern &quot;c&quot ; usage?!!

        "Medvedev" <3D.v.World@gma il.comwrote in message
        news:ce4f3ac2-33fb-4cff-b631-3d0ec872d51b@e5 3g2000hsa.googl egroups.com...
        On Jul 6, 2:12 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
        >"Medvedev" <3D.v.Wo...@gma il.comwrote in message
        >>
        >news:7fcf3a5 6-f5a7-457d-a87b-60429656b943@t5 4g2000hsg.googl egroups.com...
        >>
        What's that preprocessor do
        #ifdef __cplusplus
        extern "C" {
        #endif
        .
        .
        .
        #ifdef __cplusplus
        }
        #endif
        >>
        and how you say extern "C" , i mean how u extern constant!!
        >>
        >extern "C"
        >{
        >>
        >}
        >>
        >.. causes any functions inside the braces to have
        >"C linkage", so that they can be called from C
        >functions. There's nothing about 'constant' here.
        >
        sorry , but what differ C++ functions from C ones
        C++ functions have overloading. C functions do not. This means there is
        only one definition for a C function, there can be more than one for a C++
        function. Compilers typcially handle this by "mangling". This will change
        the name of the C++ function in the object file so the linker can
        differentiate between calling parameters.

        stating:
        extern "C" {
        tells the compiler not to mangle the function names, there will only be one
        declaration for each function, so then when it is linked it has C linkage,
        the function name would be the same as if it was a C function, and C
        programs/objects can call the function as they can now link to them.


        Comment

        • Mike Wahler

          #5
          Re: extern &quot;c&quot ; usage?!!


          "Medvedev" <3D.v.World@gma il.comwrote in message
          news:ce4f3ac2-33fb-4cff-b631-3d0ec872d51b@e5 3g2000hsa.googl egroups.com...
          On Jul 6, 2:12 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
          >"Medvedev" <3D.v.Wo...@gma il.comwrote in message
          >>
          >news:7fcf3a5 6-f5a7-457d-a87b-60429656b943@t5 4g2000hsg.googl egroups.com...
          >>
          What's that preprocessor do
          #ifdef __cplusplus
          extern "C" {
          #endif
          .
          .
          .
          #ifdef __cplusplus
          }
          #endif
          >>
          and how you say extern "C" , i mean how u extern constant!!
          >>
          >extern "C"
          >{
          >>
          >}
          >>
          >.. causes any functions inside the braces to have
          >"C linkage", so that they can be called from C
          >functions. There's nothing about 'constant' here.
          >
          sorry , but what differ C++ functions from C ones
          Think about overloading. You can have more than a
          single function with the same name (the number and/or
          types of arguments would differ.

          int foo(void);
          int foo(int);
          int foo(int,int);

          C++ needs to distinguish these functions from one another.
          There's no specific method required by the standard, but
          typically the compiler will 'mangle' the names (change and/or
          add characters) so that they become unique.

          "extern C" prevents this 'mangling', so that the function
          can be linked with C (using its 'real' name).

          Anyway, I should have already cited this link from the C++ FAQ:


          This explains it better than I.

          -Mike



          Comment

          • James Kanze

            #6
            Re: extern &quot;c&quot ; usage?!!

            On Jul 7, 12:18 am, Medvedev <3D.v.Wo...@gma il.comwrote:
            On Jul 6, 2:12 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
            "Medvedev" <3D.v.Wo...@gma il.comwrote in message
            news:7fcf3a56-f5a7-457d-a87b-60429656b943@t5 4g2000hsg.googl egroups.com....
            What's that preprocessor do
            #ifdef __cplusplus
            extern "C" {
            #endif
            .
            .
            .
            #ifdef __cplusplus
            }
            #endif
            and how you say extern "C" , i mean how u extern constant!!
            extern "C"
            {
            }
            .. causes any functions inside the braces to have
            "C linkage", so that they can be called from C
            functions. There's nothing about 'constant' here.
            sorry , but what differ C++ functions from C ones
            Whatever the implementation wants. There's no fundamental
            reason for two different languages to use the same calling
            conventions. One typical difference might be that in C++, the
            call stack is cleaned up in the called function (since it
            involves calling destructors, etc.), where as in C, it is
            cleaned up in the callee (since historically, C didn't have
            prototypes, and allowed calling a function with extra arguments,
            which were ignored). Also, C++ has overloading, which means
            that some sort of information concerning the type and number of
            arguments must be maintained in the object file. And because C
            allowed extra arguments, a C compiler will not want to do this.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            Working...