Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Link C++ object with C object

Reply
Thread Tools

Link C++ object with C object

 
 
Dominik Werder
Guest
Posts: n/a
 
      09-09-2004
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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-09-2004

"Dominik Werder" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      09-09-2004
On 9 Sep 2004 03:05:06 -0700 in comp.lang.c++, http://www.velocityreviews.com/forums/(E-Mail 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/

 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      09-10-2004
In article <(E-Mail Removed) >,
Dominik Werder <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
RE: Link Link Link =?Utf-8?B?REw=?= Windows 64bit 0 05-17-2005 12:15 PM
Re: Link Link Link DANGER WILL ROBINSON!!! Kevin Spencer ASP .Net 0 05-17-2005 10:41 AM
D-LINK DWL-AG650 doesn't work with WinXP - Zero Configuration Service (WZCSVC) / D-Link AirXpert nearly works Johannes Rosenstock Wireless Networking 0 08-27-2004 11:49 PM



Advertisments