473,387 Members | 1,757 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,387 software developers and data experts.

linking C with C++

Can someone please explain to me why when linking a C with a C++ program
that there has to be some flag to check to see if it is compiled with C++.

For example. This is not exactly correct but hopefully gives the idea.

Thanks in advance.
#ifdef _cplusplus

extern c

{

}

#endif
Jul 22 '05 #1
7 1522
"johny smith" <pr**************@charter.net> wrote in
news:10*************@corp.supernews.com:
Can someone please explain to me why when linking a C with a C++
program that there has to be some flag to check to see if it is
compiled with C++.


Unlike C compilers, C++ compilers need, when generating an object file,
to "decorate" the symbols with type information in order to support
overloading (using the same symbol for two or more functions with
different parameter types). Since C does not support overloading like
this, C compilers do not generate this decoration.

The extern "C" tells the C++ compiler not to adorn symbol names with type
information, but insted to store the symbol the way a C compiler would.
Of course, functions declared this way will not support overloading, and
must be non-member or static member functions.

The #ifdef __cplusplus is required because only the C++ compiler
understands the extern "C" declaration. You would normally see something
like this:

#ifdef __cplusplus
extern "C" {
#endif

/* stuff to be used by both C and C++ here */

#ifdef __cplusplus
}
#endif

Hope this helps.

Gregg
Jul 22 '05 #2
Gregg <gr***@invalid.invalid> wrote in
news:Xn*********************************@207.69.15 4.203:
The #ifdef __cplusplus is required because only the C++ compiler
understands the extern "C" declaration. You would normally see
something like this:

#ifdef __cplusplus
extern "C" {
#endif

/* stuff to be used by both C and C++ here */

#ifdef __cplusplus
}
#endif


Let me clarify. The #ifdef __cplusplus allows the same header to be
included by both C and C++ source files. The #ifdef __cplusplus is not
required if the header is going to be included only by a C++ source file
and never by a C source file.

Gregg
Jul 22 '05 #3
Gregg wrote:

"johny smith" <pr**************@charter.net> wrote in
news:10*************@corp.supernews.com:
Can someone please explain to me why when linking a C with a C++
program that there has to be some flag to check to see if it is
compiled with C++.


Unlike C compilers, C++ compilers need, when generating an object file,
to "decorate" the symbols with type information in order to support
overloading (using the same symbol for two or more functions with
different parameter types). Since C does not support overloading like
this, C compilers do not generate this decoration.

The extern "C" tells the C++ compiler not to adorn symbol names with type
information, but insted to store the symbol the way a C compiler would.
Of course, functions declared this way will not support overloading, and
must be non-member or static member functions.


Member functions cannot have C linkage, even if static (the compiler will
ignore the extern "C" {...} applied to them).

Interestingly, the standard avoids specifying what a language linkage
(either C++ or C) must or should entail, leaving it to the implementation.
So I suppose that instead of C++ name mangling we could have had some other
mechanism. I also read it to mean that, in principle, C and C++ linkages
are allowed to use different calling conventions. At any rate, it is
undefined behaviour to call a function through an expression with a different
language linkage; e.g., to use a pointer to a static member function where
a "C" function is expected.
(I have a vague idea of the real world implications of the last rule, but
I don't see a reason not to follow it).

Denis
Jul 22 '05 #4
Denis Remezov <RE*********************@yahoo.removethis.ca> wrote in
news:40***************@yahoo.removethis.ca:

Member functions cannot have C linkage, even if static (the compiler
will ignore the extern "C" {...} applied to them).

Yes, that is what the standard says. I'm not sure what I was thinking of
when I wrote that, but it may have been the idea of using a static member
function as the target of a callback from code expecting a C function.
That of course does not involve symbolic linkage, but does involve ABI
compatibility (stack frame layout, register usage, and other calling
conventions).
Interestingly, the standard avoids specifying what a language linkage
(either C++ or C) must or should entail, leaving it to the
implementation. So I suppose that instead of C++ name mangling we
could have had some other mechanism. I also read it to mean that, in
principle, C and C++ linkages are allowed to use different calling
conventions. At any rate, it is undefined behaviour to call a
function through an expression with a different language linkage;
e.g., to use a pointer to a static member function where a "C"
function is expected. (I have a vague idea of the real world
implications of the last rule, but I don't see a reason not to follow
it).


That is all technically true, but practically speaking, it is name-
mangling that is the significant difference when mixing C and C++ (at
least when using compilers from the same vendor).

Gregg
Jul 22 '05 #5
Interestingly, the standard avoids specifying what a language linkage
(either C++ or C) must or should entail, leaving it to the
implementation. So I suppose that instead of C++ name mangling we
could have had some other mechanism. I also read it to mean that, in
principle, C and C++ linkages are allowed to use different calling
conventions. At any rate, it is undefined behaviour to call a
function through an expression with a different language linkage;
e.g., to use a pointer to a static member function where a "C"
function is expected. (I have a vague idea of the real world
implications of the last rule, but I don't see a reason not to follow
it).


That is all technically true, but practically speaking, it is name-
mangling that is the significant difference when mixing C and C++ (at
least when using compilers from the same vendor).


Not sure it is true even in a majority of cases. For instance, in my latest
project I had to access
a third party DLL exporting "C" functions from my C++ program using
GetProcAddress(). The function was resolved
by name just fine, but when trying to call it I was getting that MS runtime
( ESA (sp?)) error about calling convention until I typedef-ed
the type that I was using to store the pointer to resolved function with
"extern "C"" . Then it worked.

Sergei
Jul 22 '05 #6
Gregg <gr***@invalid.invalid> wrote in message news:<Xn********************************@207.69.15 4.202>...

[ ... ]
That is all technically true, but practically speaking, it is name-
mangling that is the significant difference when mixing C and C++ (at
least when using compilers from the same vendor).


That's not really accurate -- quite a few compilers typically use
different calling conventions between the two languages. Since C can
be written without prototypes, C compilers often default to argument
passing that can withstand differences between the number of arguments
passed and number of arguments expected by the function.

In C++, function declarations are required. Since we know the
arguments in the call match those expected by the function, they can
use a more efficient calling convention.

In reality, the name mangling mostly just prevents you from making
calls that would just crash and burn anyway.
--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #7
Jerry Coffin wrote:
[..] Since C can
be written without prototypes, [...]


Not since 1999 it can't.

V
Jul 22 '05 #8

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

Similar topics

0
by: Wolfgang | last post by:
I have a problem with linking my CPP Code under a irix6 machine (sgi, UNIX). In my CPP code I use some Functions which are written in Python. So its a kind of CPP wrapper for my Python functions In...
0
by: Dibyendu Roy | last post by:
Hi All, I build an object called "dblorcle" to connect to oracle database in Sun solaris box. This is built linking with various oracle ".a" (archived, for static linking) files come with standard...
7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
2
by: sunil | last post by:
Hi, We have lot of c and fortran archive libraries that have complex dependencies. We have different server tasks that use some of these libraries. We have developed a tool inhouse that links...
20
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external...
0
by: gasturbtec | last post by:
please help im new at access programming and i just got this project dropped in my lap because the old programmer quit. i've been doing ok so far but now i need to add code to an existing database...
6
by: Rudy Ray Moore | last post by:
I work with a multi-project workspace. One project (the "startup" project) has a "Configuration Type" of "Application (.exe)". The other 40 projects have a "Configuration Type" of "Static Library...
0
by: Rudy Ray Moore | last post by:
I've been having trouble getting incremental linking to work under Visual C++ .net 2003 7.1 for my multi-project workspace. Ronald Laeremans and Carl Daniel (and a few others) helped me figure it...
0
by: Philip Lowman | last post by:
I am in the process of trying to migrate a couple of build solutions to Visual Studio Express 2005 from VS 2003 Professional and I am running into a weird C/C++ runtime library linking issue when...
1
by: srikar | last post by:
what is the difference between static linking & dynamic linking, what are the advantages of each? How to perform static linking & Dynamic linking by using gcc -o liniking will be done , but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...

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.