f



Link C++ object with C object

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
0
dwerder (37)
9/9/2004 10:05:06 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

3 Replies
879 Views

Similar Articles

[PageSpeed] 29

"Dominik Werder" <dwerder@gmx.net> wrote in message
news:31033452.0409090205.23ec5ae5@posting.google.com...
> 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


0
9/9/2004 10:22:40 AM
On 9 Sep 2004 03:05:06 -0700 in comp.lang.c++, dwerder@gmx.net (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/

0
source1 (137)
9/9/2004 5:53:59 PM
In article <31033452.0409090205.23ec5ae5@posting.google.com>,
Dominik Werder <dwerder@gmx.net> wrote:
>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
-- 
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
0
comeau (830)
9/10/2004 5:46:02 AM
Reply: