comp.lang.ada
 help / color / mirror / Atom feed
From: Mike Young <mikey@str.com>
Subject: Re: Interface between ADA and C++
Date: 1996/05/23
Date: 1996-05-23T00:00:00+00:00	[thread overview]
Message-ID: <31A51616.3222EAAC@str.com> (raw)
In-Reply-To: 31A46A6E.4B2F@banana.demon.co.uk


Kevin F. Quinn wrote:
> 
> Norbert Roeben wrote:
> 
> > I try to call an C++ function from Ada. The linker always searches for
> > C - function, not for a C++ - function.
> 
> Your problem is that the C++ function name is mangled by the C++ compiler,
> almost certainly.
> 
> > Does anybody has a solution for this ?
> 
> Try adding something along the lines of (un-tried)
> 
> extern "C" {
>   void GlueCppHello ( char *String ) {
>      CppHello(String);
>   }
> }
> 
> to your C++ source set - the 'extern "C" {}' construct ensures that the code
> in between is treated as normal "C" code, and as such the function name
> GlueCppHello isn't mangled.  Then call GlueCppHello from your Ada code.
> 
> >         Unresolved:
> >         CppHello

================
Many C and C++ compilers prepend a '_' to the name, thusly: _CppHello. Try
that if you continue to have problems.

> 
> BTW - the above is a direct result of the change in function name by the C++
> compiler.  There are a number of good reasons for this, but this is not the
> place to go into them.  If you have complete documentation on the C++ compiler,
> it may well tell you how to determine the actual linker symbol generated, in
> which case you can use this directly instead of using the 'extern "C" ' stuff.

==========
The simple answer is not at all obscure, and it wouldn't hurt to describe
it here. The C++ compiler simply tags the function name with return type,
class name, argument signatures, access privileges, and possibly other bits
of identifying information. This allows the linker to find the correct one
of possibly many overloads. Extern C functions, OTOH, cannot be overloaded;
decorated names are not emitted for extern "C" functions.

As for guessing and using the decorated name: this is generally not a good
idea. It's a pain in the behind, for one, not portable, for two, and extern
C takes care of all this, for three.

Mike.




  reply	other threads:[~1996-05-23  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-05-22  0:00 Interface between ADA and C++ Norbert Roeben
1996-05-23  0:00 ` Kevin F. Quinn
1996-05-23  0:00   ` Mike Young [this message]
1996-05-24  0:00     ` Darren C Davenport
1996-05-29  0:00       ` Ken Slater
1996-05-29  0:00         ` Ding-yuan Sheu
1996-05-24  0:00 ` Vincent P. Amiot
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox