comp.lang.ada
 help / color / mirror / Atom feed
* Interface between ADA and C++
@ 1996-05-22  0:00 Norbert Roeben
  1996-05-23  0:00 ` Kevin F. Quinn
  1996-05-24  0:00 ` Vincent P. Amiot
  0 siblings, 2 replies; 7+ messages in thread
From: Norbert Roeben @ 1996-05-22  0:00 UTC (permalink / raw)



Hi,

I'm trying to find a way to interface ADA with C++. 
I'm using GNAT 3.03 with gcc 2.7.2 with g++ library 2.7.1.

I try to call an C++ function from Ada. The linker always searches for
C - function, not for a C++ - function.

Does anybody has a solution for this ?

Here an extract from my sources and the way I compiled them :

ADA Sources :

file hellointerface.ads :

	package HelloInterface is

	   procedure CppHello          ( Text    : String  );

	  private

	  -- Interface to a C++ - function
	  pragma Import       ( Cpp,CppHello,"CppHello");

	end HelloInterface;


file hello.adb :

	with text_io;
	with HelloInterface;

	procedure Hello is

	begin
   
	   HelloInterface.CppHello ("And it works with C++");

	end;



C++ Source :

file cppHello.cc :

	#include <iostream.h>

	void CppHello ( char *String ) {

	  cout << String << endl;

	}


The way how I compiled them:

gcc -g -c hello.adb
gcc -g -c hellointerface.ads
g++ -g -c cppHello.c
gnatbl hello.ali -o hello cppHello.o cHello.o -lg++ -lstdc++

The Error Message I got :

	No code generated for file hellointerface.ads (package spec)
	No code generated for file except.ads (package spec)
	collect2: ld returned 1 exit status
	/usr/bin/../lib/ld:
	Unresolved:
	CppHello



-- 
+-----------------+----------------------+-------------------------------+
| Norbert Roeben  | DEPARTMENT :  ETS 52 |  PHONE : (49) 421-4573695     |
+-----------------+----------------------+-------------------------------+
| EMAIL  : roeben@essiris2.atlas.de                                      |
+------------------------------------------------------------------------+
| STN ATLAS Elektronik            Brueggeweg 54            28305 Bremen  |
+------------------------------------------------------------------------+




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

* Re: Interface between ADA and C++
  1996-05-23  0:00 ` Kevin F. Quinn
@ 1996-05-23  0:00   ` Mike Young
  1996-05-24  0:00     ` Darren C Davenport
  0 siblings, 1 reply; 7+ messages in thread
From: Mike Young @ 1996-05-23  0:00 UTC (permalink / raw)



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.




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

* Re: Interface between ADA and C++
  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
  1996-05-24  0:00 ` Vincent P. Amiot
  1 sibling, 1 reply; 7+ messages in thread
From: Kevin F. Quinn @ 1996-05-23  0:00 UTC (permalink / raw)



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

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.

Cheers,
Kev.




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

* Re: Interface between ADA and C++
  1996-05-23  0:00   ` Mike Young
@ 1996-05-24  0:00     ` Darren C Davenport
  1996-05-29  0:00       ` Ken Slater
  0 siblings, 1 reply; 7+ messages in thread
From: Darren C Davenport @ 1996-05-24  0:00 UTC (permalink / raw)



Mike Young <mikey@str.com> wrote:
>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.

However, GNAT does have the ability to interface with C++, including 
deriving classes, and you can not use that capability if you use extern.
You do need to know the mangled name to use this feature.

Darren





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

* Re: Interface between ADA and C++
  1996-05-22  0:00 Interface between ADA and C++ Norbert Roeben
  1996-05-23  0:00 ` Kevin F. Quinn
@ 1996-05-24  0:00 ` Vincent P. Amiot
  1 sibling, 0 replies; 7+ messages in thread
From: Vincent P. Amiot @ 1996-05-24  0:00 UTC (permalink / raw)



Norbert Roeben wrote:
> 
> Hi,
> 
> I'm trying to find a way to interface ADA with C++.
> I'm using GNAT 3.03 with gcc 2.7.2 with g++ library 2.7.1.
> 
> I try to call an C++ function from Ada. The linker always searches for
> C - function, not for a C++ - function.
> 
> Does anybody has a solution for this ?
> 
> Here an extract from my sources and the way I compiled them :
> 
> ADA Sources :
> 
> file hellointerface.ads :
> 
>         package HelloInterface is
> 
>            procedure CppHello          ( Text    : String  );
> 
>           private
> 
>           -- Interface to a C++ - function
>           pragma Import       ( Cpp,CppHello,"CppHello");
> 
>         end HelloInterface;
> 
> file hello.adb :
> 
>         with text_io;
>         with HelloInterface;
> 
>         procedure Hello is
> 
>         begin
> 
>            HelloInterface.CppHello ("And it works with C++");
> 
>         end;
> 
> C++ Source :
> 
> file cppHello.cc :
> 
>         #include <iostream.h>
> 
>         void CppHello ( char *String ) {
> 
>           cout << String << endl;
> 
>         }
> 
> The way how I compiled them:
> 
> gcc -g -c hello.adb
> gcc -g -c hellointerface.ads
> g++ -g -c cppHello.c
> gnatbl hello.ali -o hello cppHello.o cHello.o -lg++ -lstdc++
> 
> The Error Message I got :
> 
>         No code generated for file hellointerface.ads (package spec)
>         No code generated for file except.ads (package spec)
>         collect2: ld returned 1 exit status
>         /usr/bin/../lib/ld:
>         Unresolved:
>         CppHello
> 

-- Your C++ external name must be mangled: the profile of your C++ routine
-- is combined with its human readable original name to give a funky name:

on HP with my (GNU?) C++ I get


$ nm -x cpphello.o


Symbols from cpphello.o:

Name                    Value   Scope  Type    Subspace

$global$            |          |undef |data   |
CppHello__FPc       |0x00000000|extern|entry  |$CODE$
__ls__7ostreamPCc   |          |undef |code   |
__ls__7ostreamPFR7ostream_R7ostream|          |undef |code   |
cout                |          |undef |data   |
endl__FR7ostream    |          |undef |code   |                      

 
CppHello__FPc is your 'mangled/funky' name, mangling you have guessed
it is used to enforce confomance rules (via ld) in C++.


some systems provide unmangle/demangle tools to translate the name.

Note that even if you get the name right:

   - mangling schemes may vary from C++ compiler to C++ compiler and 
     from version to version.

   - fishing out the proper name is likely not to be enough, the C++
     runtime must still coexist peacefully with the Ada RTS.

     The trick may be to offer the C++ services in DLLS/Shared Libs
     (such beasts tend to have better defined behaviors)

Have Fun

Vincent

> --
> +-----------------+----------------------+-------------------------------+
> | Norbert Roeben  | DEPARTMENT :  ETS 52 |  PHONE : (49) 421-4573695     |
> +-----------------+----------------------+-------------------------------+
> | EMAIL  : roeben@essiris2.atlas.de                                      |
> +------------------------------------------------------------------------+
> | STN ATLAS Elektronik            Brueggeweg 54            28305 Bremen  |
> +------------------------------------------------------------------------+




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

* Re: Interface between ADA and C++
  1996-05-24  0:00     ` Darren C Davenport
@ 1996-05-29  0:00       ` Ken Slater
  1996-05-29  0:00         ` Ding-yuan Sheu
  0 siblings, 1 reply; 7+ messages in thread
From: Ken Slater @ 1996-05-29  0:00 UTC (permalink / raw)



In article <4o4h0p$l32@hacgate2.hac.com>, Darren C Davenport <ddavenpo@redwood.dn.hac.com> writes:
> Mike Young <mikey@str.com> wrote:
>>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.
> 
> However, GNAT does have the ability to interface with C++, including 
> deriving classes, and you can not use that capability if you use extern.
> You do need to know the mangled name to use this feature.
> 
> Darren
> 
I have just started using GNAT on a Silicon Graphics Indy workstation and
was also wondering how easy it was to interface to C++. Do you use pragmas?
Is there any sample code showing how this is done? Is it dependent on the
C++ compiler used (i.e. must you use gcc)? 
Thanks.




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

* Re: Interface between ADA and C++
  1996-05-29  0:00       ` Ken Slater
@ 1996-05-29  0:00         ` Ding-yuan Sheu
  0 siblings, 0 replies; 7+ messages in thread
From: Ding-yuan Sheu @ 1996-05-29  0:00 UTC (permalink / raw)



Ken Slater wrote:
> 

> I have just started using GNAT on a Silicon Graphics Indy workstation and
> was also wondering how easy it was to interface to C++. Do you use pragmas?
> Is there any sample code showing how this is done? 

Yes, you can download "c++-interface" in 

"ftp://ftp.cdrom.com/pub/languages/ada/compiler/gnat/distrib/docs/"

This article has an example that shows how to interface a C++ class
from Ada programs. I still try to interface an Ada tagged type from
C++ programs but I have not successed yet.

>Is it dependent on the C++ compiler used (i.e. must you use gcc)?

No, you still can use the CC (SGI's C++ complier) and GNAT.
However, I has to reminder you that GNAT's C++ LOW-LEVEL interface
capability is defined for third party venders not for programmers.
If you want to do this kind of work by youeself, I'll say it is not
easy especially for large system.




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

end of thread, other threads:[~1996-05-29  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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