Questions about extern "C" linkage directive

Discussion in 'C++' started by mimi, Mar 22, 2007.

  1. mimi

    mimi Guest

    The programmer indicates to the compiler that a function is written
    in a different programming language using a linkage directives.It is
    intuitive that extern "SomeLanguage" is used to declare functions
    written in the "SomeLanguage".
    But I am quite confused what information does the linkage directive
    tells the compiler.The "generated" function name? The way the
    arguments are ordered?Or something else?
    And I am still wondering why extern "C" can be used to make a C++
    function available to a C program.Why not extern "C++"?
    Thanks for any advice.
     
    mimi, Mar 22, 2007
    #1
    1. Advertising

  2. On 22 Mar, 08:26, "mimi" <> wrote:
    > The programmer indicates to the compiler that a function is written
    > in a different programming language using a linkage directives.It is
    > intuitive that extern "SomeLanguage" is used to declare functions
    > written in the "SomeLanguage".
    > But I am quite confused what information does the linkage directive
    > tells the compiler.The "generated" function name? The way the
    > arguments are ordered?Or something else?
    > And I am still wondering why extern "C" can be used to make a C++
    > function available to a C program.Why not extern "C++"?
    > Thanks for any advice.


    extern "C" tells the compiler that the function should be linked using
    the C calling convention, this tells the linker how the name of the
    function should look like to the linker and how the arguments are
    passed (on the stack/in registers, in which order and such). Using
    extern "C" will among other things tell the compiler to not mangle the
    name as it usually does for C++ function (this is necessary for
    overloading IIRC).

    The reason that extern "C" makes a function available to other
    languages is that those other languages also can use the C calling
    convention while they probably don't recognize the C++ one. Why C one
    can ask, and the answer is simple, it was one of the first big
    languages and any language that wanted to be able to call C functions
    had to adopt to the C way, and thus it be came the "standard" of
    language interoperability.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 22, 2007
    #2
    1. Advertising

  3. mimi

    mimi Guest

    On Mar 22, 4:19 pm, "Erik Wikström" <>
    wrote:
    > On 22 Mar, 08:26, "mimi" <> wrote:
    >
    > > The programmer indicates to the compiler that a function is written
    > > in a different programming language using a linkage directives.It is
    > > intuitive that extern "SomeLanguage" is used to declare functions
    > > written in the "SomeLanguage".
    > > But I am quite confused what information does the linkage directive
    > > tells the compiler.The "generated" function name? The way the
    > > arguments are ordered?Or something else?
    > > And I am still wondering why extern "C" can be used to make a C++
    > > function available to a C program.Why not extern "C++"?
    > > Thanks for any advice.

    >
    > extern "C" tells the compiler that the function should be linked using
    > the C calling convention, this tells the linker how the name of the
    > function should look like to the linker and how the arguments are
    > passed (on the stack/in registers, in which order and such). Using
    > extern "C" will among other things tell the compiler to not mangle the
    > name as it usually does for C++ function (this is necessary for
    > overloading IIRC).
    >
    > The reason that extern "C" makes a function available to other
    > languages is that those other languages also can use the C calling
    > convention while they probably don't recognize the C++ one. Why C one
    > can ask, and the answer is simple, it was one of the first big
    > languages and any language that wanted to be able to call C functions
    > had to adopt to the C way, and thus it be came the "standard" of
    > language interoperability.

    I don't think the usage of extern "C" is to make a function
    available to other language.The usage should be to indicate the
    compiler that the function is written is C, so use the C calling
    convention for this function. If the function is written is
    "SomeLanguage", extern "SomeLauguage" should be used to make the
    function availble to the C++ program. If the function is written in
    Ada, it should use the extern "Ada" linkage directive,and extern
    "FORTRAN" for functions written in FORTRAN language.But only the
    extern "C" is guaranteed to be supported by all C++ implementations.
    Am I wrong? If i am right, why extern "C" but not the extern "C++"
    is used to make a C++ function available to a C program. Is someone
    choose the extern "C" but not extern "C++" in this situation while
    implementing the linkage directives for the C programming language.
    > --
    > Erik Wikström
     
    mimi, Mar 22, 2007
    #3
  4. On 22 Mar, 10:14, "mimi" <> wrote:
    > On Mar 22, 4:19 pm, "Erik Wikström" <>
    > wrote:
    >
    >
    >
    >
    >
    > > On 22 Mar, 08:26, "mimi" <> wrote:

    >
    > > > The programmer indicates to the compiler that a function is written
    > > > in a different programming language using a linkage directives.It is
    > > > intuitive that extern "SomeLanguage" is used to declare functions
    > > > written in the "SomeLanguage".
    > > > But I am quite confused what information does the linkage directive
    > > > tells the compiler.The "generated" function name? The way the
    > > > arguments are ordered?Or something else?
    > > > And I am still wondering why extern "C" can be used to make a C++
    > > > function available to a C program.Why not extern "C++"?
    > > > Thanks for any advice.

    >
    > > extern "C" tells the compiler that the function should be linked using
    > > the C calling convention, this tells the linker how the name of the
    > > function should look like to the linker and how the arguments are
    > > passed (on the stack/in registers, in which order and such). Using
    > > extern "C" will among other things tell the compiler to not mangle the
    > > name as it usually does for C++ function (this is necessary for
    > > overloading IIRC).

    >
    > > The reason that extern "C" makes a function available to other
    > > languages is that those other languages also can use the C calling
    > > convention while they probably don't recognize the C++ one. Why C one
    > > can ask, and the answer is simple, it was one of the first big
    > > languages and any language that wanted to be able to call C functions
    > > had to adopt to the C way, and thus it be came the "standard" of
    > > language interoperability.

    >
    > I don't think the usage of extern "C" is to make a function
    > available to other language. The usage should be to indicate the
    > compiler that the function is written is C, so use the C calling
    > convention for this function. If the function is written is
    > "SomeLanguage", extern "SomeLauguage" should be used to make the
    > function availble to the C++ program. If the function is written in
    > Ada, it should use the extern "Ada" linkage directive,and extern
    > "FORTRAN" for functions written in FORTRAN language.But only the
    > extern "C" is guaranteed to be supported by all C++ implementations.
    > Am I wrong?


    Yes, extern "C" does not mean that the function is written in C, it
    means that it follows the C calling convention but could be
    implemented in any language, including C++.

    > If i am right, why extern "C" but not the extern "C++"


    extern "C++" would mean that you want to use the C++ calling
    convention, however this is the default when programming in C++ so
    it's quite redundant to write.

    > is used to make a C++ function available to a C program. Is someone
    > choose the extern "C" but not extern "C++" in this situation while
    > implementing the linkage directives for the C programming language.


    There are two uses for extern "C", the first is when you are writing
    code in C++ that you want to make available for other applications.
    The other use is when you have a library (for example written in C)
    that you want to use, so you include the header-file but wrap the
    include in extern "C" to indicate that all functions declared in the
    header should be called with the C calling convention.

    extern "C" {
    #include "someheader.h"
    }

    Notice that the functions don't have to be written in C, could just as
    well be Python or Fotran.

    PS. Remove signatures when replying.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 22, 2007
    #4
  5. mimi

    mimi Guest

    On Mar 22, 5:57 pm, "Erik Wikström" <>
    wrote:
    > On 22 Mar, 10:14, "mimi" <> wrote:
    >
    >
    >
    >
    >
    > > On Mar 22, 4:19 pm, "Erik Wikström" <>
    > > wrote:

    >
    > > > On 22 Mar, 08:26, "mimi" <> wrote:

    >
    > > > > The programmer indicates to the compiler that a function is written
    > > > > in a different programming language using a linkage directives.It is
    > > > > intuitive that extern "SomeLanguage" is used to declare functions
    > > > > written in the "SomeLanguage".
    > > > > But I am quite confused what information does the linkage directive
    > > > > tells the compiler.The "generated" function name? The way the
    > > > > arguments are ordered?Or something else?
    > > > > And I am still wondering why extern "C" can be used to make a C++
    > > > > function available to a C program.Why not extern "C++"?
    > > > > Thanks for any advice.

    >
    > > > extern "C" tells the compiler that the function should be linked using
    > > > the C calling convention, this tells the linker how the name of the
    > > > function should look like to the linker and how the arguments are
    > > > passed (on the stack/in registers, in which order and such). Using
    > > > extern "C" will among other things tell the compiler to not mangle the
    > > > name as it usually does for C++ function (this is necessary for
    > > > overloading IIRC).

    >
    > > > The reason that extern "C" makes a function available to other
    > > > languages is that those other languages also can use the C calling
    > > > convention while they probably don't recognize the C++ one. Why C one
    > > > can ask, and the answer is simple, it was one of the first big
    > > > languages and any language that wanted to be able to call C functions
    > > > had to adopt to the C way, and thus it be came the "standard" of
    > > > language interoperability.

    >
    > > I don't think the usage of extern "C" is to make a function
    > > available to other language. The usage should be to indicate the
    > > compiler that the function is written is C, so use the C calling
    > > convention for this function. If the function is written is
    > > "SomeLanguage", extern "SomeLauguage" should be used to make the
    > > function availble to the C++ program. If the function is written in
    > > Ada, it should use the extern "Ada" linkage directive,and extern
    > > "FORTRAN" for functions written in FORTRAN language.But only the
    > > extern "C" is guaranteed to be supported by all C++ implementations.
    > > Am I wrong?

    >
    > Yes, extern "C" does not mean that the function is written in C, it
    > means that it follows the C calling convention but could be
    > implemented in any language, including C++.
    >
    > > If i am right, why extern "C" but not the extern "C++"

    >
    > extern "C++" would mean that you want to use the C++ calling
    > convention, however this is the default when programming in C++ so
    > it's quite redundant to write.
    >
    > > is used to make a C++ function available to a C program. Is someone
    > > choose the extern "C" but not extern "C++" in this situation while
    > > implementing the linkage directives for the C programming language.

    >
    > There are two uses for extern "C", the first is when you are writing
    > code in C++ that you want to make available for other applications.
    > The other use is when you have a library (for example written in C)
    > that you want to use, so you include the header-file but wrap the
    > include in extern "C" to indicate that all functions declared in the
    > header should be called with the C calling convention.
    >
    > extern "C" {
    > #include "someheader.h"
    >
    > }
    >
    > Notice that the functions don't have to be written in C, could just as
    > well be Python or Fotran.
    >
    > PS. Remove signatures when replying.
    >
    > --
    > Erik Wikström- Hide quoted text -
    >
    > - Show quoted text -


    Thanks for your patiently reply.Your first reply had explained it
    quite clearly, it is me that didn't quite get it.
    I get it now. Thanks very much.
    For anyone who is reading this news and wonder to know what exactly
    calling convention mean, I found this article in the web.
    http://www.codeproject.com/cpp/calling_conventions_demystified.asp
     
    mimi, Mar 23, 2007
    #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. usr2003
    Replies:
    4
    Views:
    632
    usr2003
    Sep 19, 2003
  2. Steve Kobes

    extern and linkage

    Steve Kobes, Jun 9, 2004, in forum: C Programming
    Replies:
    1
    Views:
    307
    Chris Torek
    Jun 9, 2004
  3. Ian
    Replies:
    22
    Views:
    1,017
  4. Mark A. Gibbs

    Static linkage and extern "C"

    Mark A. Gibbs, Oct 20, 2005, in forum: C++
    Replies:
    10
    Views:
    802
    Mark A. Gibbs
    Oct 21, 2005
  5. Replies:
    1
    Views:
    609
    Michael DOUBEZ
    Sep 12, 2008
Loading...

Share This Page