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 4 10008
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.
"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.c c" (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
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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
On Sun, 21 Sep 2003 19:25:38 GMT, Jack Klein <ja*******@spam cop.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
// test_const.h
#ifndef HEADER_TEST
#define HEADER_TEST
|
by: Richard Hayden |
last post by:
Hi,
I have the following code:
/******************************** file1.c
#include <iostream>
extern void dummy();
inline int testfunc() {
|
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 compiled
using a C++ compiler (gcc version 3.3.1). Given the differences in the
way C and C++ code gets linked in, what should I do to ensure that the
C code that links in the library does not give any linker error?
Thanks,
Gus
|
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*));
|
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
function-prototype scope
I think(might be wrong):
| |
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 a variable with
local scope, external linkage, and static storage.
I believe I can do this for the file that does not define the variable
by declaring it inside a function. Can I also avoid giving it file
scope in the file that does define the...
|
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 inline may have
external linkage while in others it has internal (from what I have read
in comments by people). Are there any (other) places where linkage is
ambiguous?
|
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 object or a function
contains the storage-class specifier extern , the identifier has the
same linkage as any visible declaration of the identifier with file
scope. If there is no visible declaration with file scope, the
identifier has external...
|
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 variable, but according to
the standard there is a difference:
"While this is essentially true in practical effect, there are subtle
differences. Using static as shown here causes "i" to have internal
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |