public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Can I use C and C++ objects in one program?
@ 1999-12-14 19:53 dpace
  1999-12-15  2:23 ` Johnny Favorite (it means "Reality Cop")
  1999-12-31 22:24 ` dpace
  0 siblings, 2 replies; 4+ messages in thread
From: dpace @ 1999-12-14 19:53 UTC (permalink / raw)
  To: help-gcc

I am linking a .cpp piece of code with some .c files.
But, I get a list of functions not found as though
the .o files can't see each other's functions.

Any suggestions?
I don't see any special gcc or g++ options for this.


--
David Pace - Free commodity/stock graphing software
and Linux links at http://www.daveware.com



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can I use C and C++ objects in one program?
  1999-12-14 19:53 Can I use C and C++ objects in one program? dpace
@ 1999-12-15  2:23 ` Johnny Favorite (it means "Reality Cop")
  1999-12-31 22:24   ` Johnny Favorite (it means "Reality Cop")
  1999-12-31 22:24 ` dpace
  1 sibling, 1 reply; 4+ messages in thread
From: Johnny Favorite (it means "Reality Cop") @ 1999-12-15  2:23 UTC (permalink / raw)
  To: help-gcc

dpace wrote:
> I am linking a .cpp piece of code with some .c files.
> But, I get a list of functions not found as though
> the .o files can't see each other's functions.

Welcome to the Wonderful World of Name Mangling.

In C++ you can have several functions that all have the same name but take
different parameters.  But it is a requirement that each function have a
UNIQUE name when it gets into the object file.  Hence, name mangling.  Each
C++ function's name will be "decorated" with funny characters describing its
parameters and return type and what class it belongs to when it hits the
object file.

C code knows nothing of this name mangling and so it will not be able to
call C++ functions.  In C++ there is sort of a workaround.  When you make a
declaration for a C function in a C++ header file, do it like this:

extern "C"
  {
    void functionName(int i);
  }

Then the C++ code will know that this name is NOT mangled and so it can call
it properly using the C calling convention.

That's kind of the hard way, though.  An easier way is to change the
extensions of all your .c files to .cpp, chances are excellent that they
will compile fine with few or no changes.  Then all your code will be
compiled according to the C++ rules, all functions will be name-mangled, and
all the code will get along fine.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Can I use C and C++ objects in one program?
  1999-12-14 19:53 Can I use C and C++ objects in one program? dpace
  1999-12-15  2:23 ` Johnny Favorite (it means "Reality Cop")
@ 1999-12-31 22:24 ` dpace
  1 sibling, 0 replies; 4+ messages in thread
From: dpace @ 1999-12-31 22:24 UTC (permalink / raw)
  To: help-gcc

I am linking a .cpp piece of code with some .c files.
But, I get a list of functions not found as though
the .o files can't see each other's functions.

Any suggestions?
I don't see any special gcc or g++ options for this.


--
David Pace - Free commodity/stock graphing software
and Linux links at http://www.daveware.com



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can I use C and C++ objects in one program?
  1999-12-15  2:23 ` Johnny Favorite (it means "Reality Cop")
@ 1999-12-31 22:24   ` Johnny Favorite (it means "Reality Cop")
  0 siblings, 0 replies; 4+ messages in thread
From: Johnny Favorite (it means "Reality Cop") @ 1999-12-31 22:24 UTC (permalink / raw)
  To: help-gcc

dpace wrote:
> I am linking a .cpp piece of code with some .c files.
> But, I get a list of functions not found as though
> the .o files can't see each other's functions.

Welcome to the Wonderful World of Name Mangling.

In C++ you can have several functions that all have the same name but take
different parameters.  But it is a requirement that each function have a
UNIQUE name when it gets into the object file.  Hence, name mangling.  Each
C++ function's name will be "decorated" with funny characters describing its
parameters and return type and what class it belongs to when it hits the
object file.

C code knows nothing of this name mangling and so it will not be able to
call C++ functions.  In C++ there is sort of a workaround.  When you make a
declaration for a C function in a C++ header file, do it like this:

extern "C"
  {
    void functionName(int i);
  }

Then the C++ code will know that this name is NOT mangled and so it can call
it properly using the C calling convention.

That's kind of the hard way, though.  An easier way is to change the
extensions of all your .c files to .cpp, chances are excellent that they
will compile fine with few or no changes.  Then all your code will be
compiled according to the C++ rules, all functions will be name-mangled, and
all the code will get along fine.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-12-31 22:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-12-14 19:53 Can I use C and C++ objects in one program? dpace
1999-12-15  2:23 ` Johnny Favorite (it means "Reality Cop")
1999-12-31 22:24   ` Johnny Favorite (it means "Reality Cop")
1999-12-31 22:24 ` dpace

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).