473,761 Members | 2,384 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 "SomeLangua ge" is used to declare functions
written in the "SomeLangua ge".
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 6259
On 22 Mar, 08:26, "mimi" <cainiaodelixi. ..@gmail.comwro te:
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 "SomeLangua ge" is used to declare functions
written in the "SomeLangua ge".
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 interoperabilit y.

--
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.comwro te:
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 "SomeLangua ge" is used to declare functions
written in the "SomeLangua ge".
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 interoperabilit y.
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
"SomeLangua ge", extern "SomeLaugua ge" 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.comwro te:
On Mar 22, 4:19 pm, "Erik Wikström" <eri...@student .chalmers.se>
wrote:


On 22 Mar, 08:26, "mimi" <cainiaodelixi. ..@gmail.comwro te:
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 "SomeLangua ge" is used to declare functions
written in the "SomeLangua ge".
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 interoperabilit y.

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
"SomeLangua ge", extern "SomeLaugua ge" 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.comwro te:


On Mar 22, 4:19 pm, "Erik Wikström" <eri...@student .chalmers.se>
wrote:
On 22 Mar, 08:26, "mimi" <cainiaodelixi. ..@gmail.comwro te:
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 "SomeLangua ge" is used to declare functions
written in the "SomeLangua ge".
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 interoperabilit y.
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
"SomeLangua ge", extern "SomeLaugua ge" 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
2650
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
1464
by: Phlip | last post by:
Language Lawyers: Compare this: extern "C" int Maine() { ... bool runTests(); return !runTests(); }
22
6027
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 given extern "C" linkage where it is to be assigned to a C function pointer. Ian
10
6193
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: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
12
2730
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 linkage? Not much on this in the FAQ or tutorials.
19
3860
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 storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
4
4757
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 global function with the extern "C" prefix. I was leaning toward using the static method approach until I saw a posting that said compatibility between static C++ functions and C is not guaranteed in the respective language standards. This made me...
3
2103
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 two files test.h and foo.c. a function body int foo(int arg) { //declarations }
7
1770
by: Rahul | last post by:
Hi Everyone, I have the following code, void f(int &) { printf("in f...\n"); } #ifdef __cplusplus
0
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8770
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.