Click to See Complete Forum and Search --> : header definitions question (extern "C")


indiocolifa
January 31st, 2003, 08:51 AM
:confused:

i´m compiling a c++ program which uses some C libraries (pc.h, bios.h) in GCC 3.2 and the linker fails with "multiple redefinition" error in many of my program functions (seems a conflict between .cpp and .h files)

what is #ifdef c_plus_plus and extern "c"?

can be avoided the problem with this definitions?

thank you.

also, give some recommendation on general .h and .cpp coding conventions such as using, for example,

#ifndef __PROG_H__
#include __PROG_H__
#endif

Wfranc
January 31st, 2003, 10:15 AM
I think you should put up some code or you should be more specific about your problem






Regards,

indiocolifa
February 1st, 2003, 09:07 AM
Ive fixed the problem with the linker: I excluded from the DJGPP IDE the linking for ".h" files.

But im still wanting an explanation of this commonly seen definitions in .h files:


#ifdef _cplusplus
extern"c" {

...

#endif...


thank you in advance.

vinodp
February 1st, 2003, 01:43 PM
I couldn't get it what problem u have & what did u fix ?

-------------------------------------------------------------
#ifdef _cplusplus
extern"c" {

...

#endif...
-------------------------------------------------
C++ compiler decorates the function name (changes name) which is called name mangling
The above code generally use to prevent the name mangling of the C++ compiler. so if ur compiling with C++ compiler it stil going to act as C declartion


Vinod

PaulWendt
February 1st, 2003, 04:40 PM
The reason C++ does "name mangling" is because C++ provides
function overloading, whereas C does not. Function overloading
is the process by which you give multiple functions the same
name, but they would vary by their arguments.

As an example:

void print(int i);
void print(char c);
void print(float f);
// etc....


Thus, you could do the following:

int i = 0;
float f = 0.0;
char c = 'c';

print(i);
print(f);
print(c);


Each function IS different, so what should the compiler do? It
"mangles" each function so that it basically creates something to
the effect of:

print_int
print_float
print_char


Each compiler's method of name mangling will be different.

--Paul

gtrdhyft
February 3rd, 2003, 06:10 PM
Each compiler's method of name mangling will be different.

--Paul


Which is true. Which is the reason C++ libraries compiled from different compilers, or even different version of the same compilers, are NOT compatible with each other. This is also the reason when you are providing third party tools or SDK library, you want to strictly stick to a C interface API, not a C++ interface.

The C++ standard failed to provide a standard for the name mangling while at the same time C++ requires name mangling to be mandatory, making C++ an incomplete language.