473,246 Members | 1,481 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,246 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 9974
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.