comp.lang.ada
 help / color / mirror / Atom feed
* Ada/C++ interface (SPARC Works)
@ 1995-04-19  0:00 Deepak Kumar
  1995-04-20  0:00 ` James Hopper
  1995-04-22  0:00 ` R. William Beckwith
  0 siblings, 2 replies; 3+ messages in thread
From: Deepak Kumar @ 1995-04-19  0:00 UTC (permalink / raw)


Has anyone tried to interface/use C++ code through Ada programs successfully
using the SUN Ada and SUN C++ compilers?

I have some code that uses a C library function, I compile this with the
SPARC C++ compiler and it doesn't link with the Ada code. if I compile
it with GCC it links just fine.

SUN has not been very helpful (what is new!) and very slow in responding.

Thanks!

Deepak.

-- 
Deepak Kumar
Department of Mathematics & Computer Science
Bryn Mawr College
Bryn Mawr, PA 19010




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

* Re: Ada/C++ interface (SPARC Works)
  1995-04-19  0:00 Ada/C++ interface (SPARC Works) Deepak Kumar
@ 1995-04-20  0:00 ` James Hopper
  1995-04-22  0:00 ` R. William Beckwith
  1 sibling, 0 replies; 3+ messages in thread
From: James Hopper @ 1995-04-20  0:00 UTC (permalink / raw)



>I have some code that uses a C library function, I compile this with the
>SPARC C++ compiler and it doesn't link with the Ada code. if I compile
>it with GCC it links just fine.
>
Here is a piece from a doc i wrote for our people on interfacing
verdix (read sun ada to sun C++) not the extern added to the
front of the c declaration this is required for C++ to use c
conventions so it will link with Ada

 	procedure C_Put(Value : in Integer);   -- Ada

	extern "C" void c_put(int value)       // C++

The pragma interface and pragma interface_Name statements are
added to the Ada source to define  for the benefit of the linker
that this is an external procedure written in C, and its
external name.  The package language is a Verdix package with
defines among other things the prefix that the C compiler adds
to a function name. The pragmas are defined as follows:

	pragma Interface(C, C_Put);
	pragma Interface_name(C_Put, language.c_prefix & "c_put");

The extern statement is added to the C++ function definition
statement to require the compiler produce a C style interface to
the function for the use of the linker.  The Verdix Ada language
interface supports C but not C++ directly, thus the need for the
extern "C" statement.

It should be noted that pragma Interface_name is a Verdix
extension to the Ada language and may be different for other
compilers.

The full Ada and C source code for this example, as well as the
commands to compile and link them is presented below.  The
example is completed by presenting a sample run.

Ada Source for Test #1
with Text_IO;
procedure Test1 is 

  procedure C_Put(Value : in  Integer); 
  pragma Interface (C, C_Put); 
  pragma Interface_Name (C_Put,  language.c_prefix & "c_put"); 
    
  Value : Integer := 22;

begin  -- test1
  Text_IO.Put_Line("Starting Test #1"); 
  Text_IO.Put_Line("Calling C function with value = " &
                   Integer'Image(Value)); 
  C_Put(Value); 
  Text_IO.Put_Line("Back from C function");
end Test1;

C Source for Test #1
#include <stdio.h>;
extern "C" void c_put(int value)
{
   printf("Inside C_Put\nValue = %i\n",value);
}

Compile/Link commands for Test #1
Ada Test1.a
CC -c test1.c
a.ld test1 test1.o

Execution of Test #1
> a.out
Starting Test #1
Calling C function with value =  22
Inside C_Put
Value = 22
Back from C function




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

* Re: Ada/C++ interface (SPARC Works)
  1995-04-19  0:00 Ada/C++ interface (SPARC Works) Deepak Kumar
  1995-04-20  0:00 ` James Hopper
@ 1995-04-22  0:00 ` R. William Beckwith
  1 sibling, 0 replies; 3+ messages in thread
From: R. William Beckwith @ 1995-04-22  0:00 UTC (permalink / raw)


Deepak Kumar (dkumar@blackcat) wrote:
: Has anyone tried to interface/use C++ code through Ada programs successfully
: using the SUN Ada and SUN C++ compilers?

: I have some code that uses a C library function, I compile this with the
: SPARC C++ compiler and it doesn't link with the Ada code. if I compile
: it with GCC it links just fine.

C++ compilers mangle function names.  You either must have or define a C only
interface.  You can tell the C++ compiler not to mangle certain C code:

extern "C" {

    ...

};

Then compile the code with the Sun C++ compiler.  Your Ada pragma's can
then resolve to the unmangled names.




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

end of thread, other threads:[~1995-04-22  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-04-19  0:00 Ada/C++ interface (SPARC Works) Deepak Kumar
1995-04-20  0:00 ` James Hopper
1995-04-22  0:00 ` R. William Beckwith

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