comp.lang.ada
 help / color / mirror / Atom feed
From: James Hopper <jim_Hopper@dayton.saic.com>
Subject: Re: Ada/C++ interface (SPARC Works)
Date: 1995/04/20
Date: 1995-04-20T00:00:00+00:00	[thread overview]
Message-ID: <3n5vfh$ai3@dayuc.dayton.saic.com> (raw)
In-Reply-To: dkumar.798311808@blackcat


>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




  reply	other threads:[~1995-04-20  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-04-19  0:00 Ada/C++ interface (SPARC Works) Deepak Kumar
1995-04-20  0:00 ` James Hopper [this message]
1995-04-22  0:00 ` R. William Beckwith
replies disabled

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