By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Questions about extern "C" linkage directive

P: n/a
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.

Mar 22 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 22 Mar, 08:26, "mimi" <cainiaodelixi...@gmail.comwrote:
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

Mar 22 '07 #2

P: n/a
On Mar 22, 4:19 pm, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
On 22 Mar, 08:26, "mimi" <cainiaodelixi...@gmail.comwrote:
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

Mar 22 '07 #3

P: n/a
On 22 Mar, 10:14, "mimi" <cainiaodelixi...@gmail.comwrote:
On Mar 22, 4:19 pm, "Erik Wikström" <eri...@student.chalmers.se>
wrote:


On 22 Mar, 08:26, "mimi" <cainiaodelixi...@gmail.comwrote:
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

Mar 22 '07 #4

P: n/a
On Mar 22, 5:57 pm, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
On 22 Mar, 10:14, "mimi" <cainiaodelixi...@gmail.comwrote:


On Mar 22, 4:19 pm, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
On 22 Mar, 08:26, "mimi" <cainiaodelixi...@gmail.comwrote:
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/calli...emystified.asp

Mar 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.