Questions about extern "C" linkage directive

M

mimi

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

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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

mimi

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

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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

mimi

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


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.


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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top