User forums > Help

Linking object files does not work correctly

<< < (2/2)

Querente:
Yeah, I know CB is more correct , but for someone lazy as well, it is a slight irritation :).  I mean ,I am so lazy I even dont put "extern C {  } ", linker flag in my source files when I mix them. (Which is one of the problems I guess).

But thanks for the help ,atleast I know where the problem lies and how to deal with it :)

mandrav:
No problems, I 'm glad you got it working :)

Urxae:

--- Quote from: Querente on October 14, 2005, 01:08:56 pm ---I mean ,I am so lazy I even dont put "extern C {  } ", linker flag in my source files when I mix them. (Which is one of the problems I guess).

--- End quote ---

I was just about to post that was the problem here. If you want to use a C function from C++, you need to have it declared extern "C" (while compiling C++). The usual way to do this is to write headers like this:

--- Code: ---#ifndef HEADERNAME_H
#define HEADERNAME_H

/* Some #includes here perhaps */

#ifdef __cplusplus
extern "C" {
#endif

/* C declarations here */

#ifdef __cplusplus
}
#endif

#endif // HEADERNAME_H

--- End code ---

If you don't do this, the C++ compiler will create a call to the mangled name of the function instead of to the "C name". Since the function is actually compiled with a C compiler, it'll generate the function with the unmangled C name. Which means the linker will be looking for the function under the wrong name.

Navigation

[0] Message Index

[*] Previous page

Go to full version