473,508 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Questions about extern "C" linkage directive

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
4 6239
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2635
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
1
1451
by: Phlip | last post by:
Language Lawyers: Compare this: extern "C" int Maine() { ... bool runTests(); return !runTests(); }
22
5961
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
10
6137
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
12
2697
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
19
3821
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
4
4716
by: kk_oop | last post by:
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a...
3
2080
by: sarang | last post by:
hi, i m sarang the new member of 'thescripts'. i want to know how to use extern directive with function. i can better explain my problem in following way: I saw in one of the project, there was...
7
1754
by: Rahul | last post by:
Hi Everyone, I have the following code, void f(int &) { printf("in f...\n"); } #ifdef __cplusplus
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7385
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.