This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: how to comilate cpp with object file
- To: 'Benoît ALLOMBERT' <benoit dot allombert at synomia dot fr>
- Subject: RE: how to comilate cpp with object file
- From: "Rupert Wood" <me at rupey dot net>
- Date: Wed, 7 Nov 2001 16:46:43 -0000
- Cc: <gcc-help at gcc dot gnu dot org>
Benoît Allombert wrote:
> I dont understand why i could copilate with c and not with c++ file,
> when do:
:
> someone could explaie why it din t work!!!
Your object file exports myfunc in C-style. You need to use:
extern "c" void myfunc(char*);
or similar in your C++ source, or:
extern "C"
{
void myfunc(char*);
}
which is more useful for more than one function, or:
#ifdef __cplusplus
extern "C"
{
#endif
void myfunc(char*);
#ifdef __cplusplus
}
#endif
if you want source which compiles as both C and C++.
The reason is that in C++ it is valid to have both:
void myfunc(char*);
void myfunc(int);
and the compiler decides which one to call based on context. These two
functions, however, must be exported with unique names. Hence the
compiler generates 'decorated' or 'mangled' names which include return
type and parameter type, etc., e.g.:
void myfunc(char*); => _Z6myfuncPc
void myfunc(int); => _Z6myfunci
and it assumes, unless declared extern "c", that it is importing
myfunc() with such a mangled name.
Hope that helps,
Rup.