473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

C++ vs C linkage (difference between the two)

Once and for all can someone kindly tell me the difference between C
and C++ linkage. I thought I understood it till someone showed me the
other day that C functions, that would ordinarily require the
extern "C"

declaration, for correct linkage into a C++ program, don't need it if
the filename, in which they are defined, ends in .cc or .cpp! Can
anyone explain this in a rational manner?

For the record, this is what I have heard regarding the difference
between c and c++ linkage:

Function names with C++ linkage get "mangled" by a C++ compiler to
include the parameters, return type etc. Function names with C
linkage are not subjected to any such name mangling. By default the
linker will look for the "mangled name", rather than the actual name,
unless the we use extern "C". Is this explanation correct? Is there
anything else to it that I am missing?

Thanks,
Nimmi
Jul 19 '05 #1
4 9989
On 20 Sep 2003 05:53:34 -0700, ni*************@yahoo.com (Nimmi Srivastav) wrote:
Once and for all can someone kindly tell me the difference between C
and C++ linkage. I thought I understood it till someone showed me the
other day that C functions, that would ordinarily require the
extern "C"

declaration, for correct linkage into a C++ program, don't need it if
the filename, in which they are defined, ends in .cc or .cpp! Can
anyone explain this in a rational manner?
It's highly tool-dependent.

Neither the C nor the C++ standard define name mangling or machine code
level linkage.

The C++ standard, however, defines 'extern "C"' for use with C functions,
without quite defining things (iow., it's tool-dependent, but any
compiler _must_ offer a way to link with C functions via 'extern "C"').


For the record, this is what I have heard regarding the difference
between c and c++ linkage:

Function names with C++ linkage get "mangled" by a C++ compiler to
include the parameters, return type etc.
Often but not necessarily. It's one way (among many possible) to
achieve global static type-checking, required by the standard.

Function names with C linkage are not subjected to any such name
mangling.
They nearly always are, but a simpler form. Typically (but remember
this is tool-dependent) an underscore is added in front of the name.
With typical Windows compilers the mangling also depends on the calling
convention, which for such compilers is _independent_ of 'extern "C"'
(remember, the compiler only has to offer a way to call C functions
declared as 'extern "C"'; how it does that is up to the compiler), and
can include such things as a suffix '@n' where IIRC n is the amount with
which the function should adjust the stack when returning.

By default the
linker will look for the "mangled name", rather than the actual name,
unless the we use extern "C". Is this explanation correct? Is there
anything else to it that I am missing?


Yes, that it's all _very_ tool-dependent, not specified by the standard.

For specifics ask in a group related to the relevant compiler/system.

Jul 19 '05 #2
"Nimmi Srivastav" <ni*************@yahoo.com> wrote
Once and for all can someone kindly tell me the difference between C
and C++ linkage. I thought I understood it till someone showed me the
other day that C functions, that would ordinarily require the
extern "C"
declaration, for correct linkage into a C++ program, don't need it if
the filename, in which they are defined, ends in .cc or .cpp! Can
anyone explain this in a rational manner?
Example: you have files "function.c", "function.h", "main.cpp".

// function.c
#include "function.h"
int function (int x) { return x; }

//function.h
int function (int);

//main.cpp
extern "C" {
#include "function.h"
}
#include <iostream>
int main () { std::cout << f (0) << '\n'; }

Compiling "function.c" with a C compiler creates an object file
containing unmangled names. The extern "C" block around the
declaration of function () lets the C++ linker find the C names.

However, if "function.c" is renamed to "function.cc" (or .cpp) and
compiled with a C++ compiler, the extern "C" block is no longer
needed (in fact, leaving it in will cause linker errors), because
the names in the object file have mangled C++ names. Note that
this only works when the contents of the file is a well-formed
C program and also a well-formed C++ program, and that even
then it's possible for the code to mean different things in the two
languages.

It seems as though your system is set up so that files ending in .c
are automatically compiled with the C compiler and files ending
in .cpp or .cc with the C++ compiler. (Some compilers for both
languages will do this by themselves, or you can set it up with a
Makefile or in an IDE.)
For the record, this is what I have heard regarding the difference
between c and c++ linkage:

Function names with C++ linkage get "mangled" by a C++ compiler to
include the parameters, return type etc. Function names with C
linkage are not subjected to any such name mangling. By default the
linker will look for the "mangled name", rather than the actual name,
unless the we use extern "C". Is this explanation correct? Is there
anything else to it that I am missing?


(a) In the object file created by compiling a translation unit as C++,
all external symbols have C++ linkage unless you request a different
linkage.

(b) The filename extension sometimes determines which compiler
is used.

Hope that helped.
Buster
Jul 19 '05 #3
On Sat, 20 Sep 2003 13:03:18 GMT, al***@start.no (Alf P. Steinbach)
wrote in comp.lang.c++:
On 20 Sep 2003 05:53:34 -0700, ni*************@yahoo.com (Nimmi Srivastav) wrote:
Once and for all can someone kindly tell me the difference between C
and C++ linkage. I thought I understood it till someone showed me the
other day that C functions, that would ordinarily require the
extern "C"

declaration, for correct linkage into a C++ program, don't need it if
the filename, in which they are defined, ends in .cc or .cpp! Can
anyone explain this in a rational manner?


It's highly tool-dependent.

Neither the C nor the C++ standard define name mangling or machine code
level linkage.

The C++ standard, however, defines 'extern "C"' for use with C functions,
without quite defining things (iow., it's tool-dependent, but any
compiler _must_ offer a way to link with C functions via 'extern "C"').


I would be very surprised if you could come up with anything in the
C++ standard that requires a C++ implementation to actually produce
output capable of linking with output produced by any C compiler.

extern "C" provides a mechanism for a programmer to express a concept
to the compiler, and allows the compiler to attempt to provide what
the programmer wants. It describes an intent, not a requirement in
the usual sense of the word.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #4
On Sun, 21 Sep 2003 19:25:38 GMT, Jack Klein <ja*******@spamcop.net> wrote:
On Sat, 20 Sep 2003 13:03:18 GMT, al***@start.no (Alf P. Steinbach)
wrote in comp.lang.c++:
On 20 Sep 2003 05:53:34 -0700, ni*************@yahoo.com (Nimmi Srivastav) wrote:
>Once and for all can someone kindly tell me the difference between C
>and C++ linkage. I thought I understood it till someone showed me the
>other day that C functions, that would ordinarily require the
>extern "C"
>
>declaration, for correct linkage into a C++ program, don't need it if
>the filename, in which they are defined, ends in .cc or .cpp! Can
>anyone explain this in a rational manner?
It's highly tool-dependent.

Neither the C nor the C++ standard define name mangling or machine code
level linkage.

The C++ standard, however, defines 'extern "C"' for use with C functions,
without quite defining things (iow., it's tool-dependent, but any
compiler _must_ offer a way to link with C functions via 'extern "C"').


I would be very surprised if you could come up with anything in the
C++ standard that requires a C++ implementation to actually produce
output capable of linking with output produced by any C compiler.


Okay, just this once: §7.5/3: «Every implementation shall provide for
linkage to functions written in the C programming language, "C"»,
followed by an example (next time please look up things yourself).

You should not be surprised since that was a main design consideration
for C++.

So _why_ are you surprised?

extern "C" provides a mechanism for a programmer to express a concept
to the compiler, and allows the compiler to attempt to provide what
the programmer wants. It describes an intent, not a requirement in
the usual sense of the word.


That is incorrect. But the opposite extreme, that a function declared
with «extern "C"» is necessarily compatible with a C function, is also
incorrect. The requirements by the standard are as I described earlier.

Hth.,

- Alf

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
7
by: Generic Usenet Account | last post by:
I am implementing a library that can be linked in with C as well as C++ code. Obviously, the library API is functional, but in my implementation I am using STL. Because of this, my code is...
10
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: //...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
4
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want...
3
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
13
by: fctk | last post by:
source: http://rm-f.net/~orange/devel/specifications/c89-draft.html#3.1.2.2 there are two passages in this paragraph i can't fully understand: 1) "If the declaration of an identifier for an...
12
by: Taras_96 | last post by:
Hi everyone, AFAIK external linkage allows you to refer to variables/functions outside of the current translation unit. A variable in an unnamed namespace is similar to declaring a static...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.