Link C++ object with C object

D

Dominik Werder

Hi All,

I've compiled a cpp file with g++ without linking it and got an object
file.
Then I've compiled a C file with gcc and got an object file too.
Now if I try to link it to an executable (the main method is in the C
object) the linker complains about not finding the symbol for a
function declared in the C++ object.
Somebody told me that the two compilers use different symbol names for
the same function names.
He said also it would exist something like a "export this function as
C style" keyword but I didn't find anything.
Could somebody help me with this?

bye!
Dominik
 
J

John Harrison

Dominik Werder said:
Hi All,

I've compiled a cpp file with g++ without linking it and got an object
file.
Then I've compiled a C file with gcc and got an object file too.
Now if I try to link it to an executable (the main method is in the C
object) the linker complains about not finding the symbol for a
function declared in the C++ object.
Somebody told me that the two compilers use different symbol names for
the same function names.
He said also it would exist something like a "export this function as
C style" keyword but I didn't find anything.
Could somebody help me with this?

bye!
Dominik

In your C++ code

extern "C" int my_function()
{
...
}

john
 
D

David Harmon

On 9 Sep 2004 03:05:06 -0700 in comp.lang.c++, (e-mail address removed) (Dominik
Werder) wrote,
Hi All,

I've compiled a cpp file with g++ without linking it and got an object
file.
Then I've compiled a C file with gcc and got an object file too.
Now if I try to link it to an executable (the main method is in the C
object)

No, that won't work, main must be in the C++ part.
the linker complains about not finding the symbol for a
function declared in the C++ object.
Somebody told me that the two compilers use different symbol names for
the same function names.

You must use a compiler or pair of compilers that explicitly support C
and C++ comparability. I guess that gcc would do that.
He said also it would exist something like a "export this function as
C style" keyword but I didn't find anything.

Yes, of course you need that part most.

This issue is covered in Marshall Cline's C++ FAQ. See section
"[32] How to mix C and C++"It is always good to check the FAQ before
posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
G

Greg Comeau

I've compiled a cpp file with g++ without linking it and got an object
file.
Then I've compiled a C file with gcc and got an object file too.
Now if I try to link it to an executable (the main method is in the C
object) the linker complains about not finding the symbol for a
function declared in the C++ object.
Somebody told me that the two compilers use different symbol names for
the same function names.
He said also it would exist something like a "export this function as
C style" keyword but I didn't find anything.
Could somebody help me with this?

Check out http://www.comeaucomputing.com/techtalk/#externc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top