Go to the first, previous, next, last section, table of contents.


Interfacing to Other Languages

The facilities in annex B of the Ada 95 Reference Manual are fully implemented in GNAT, and in addition, a full interface to C++ is provided.

Interfacing to C

Interfacing to C with GNAT can use one of two approaches:

  1. The types in the package Interfaces.C may be used.
  2. Standard Ada types may be used directly. This may be less portable to other compilers, but will work on all GNAT compilers, which guarantee correspondence between the C and Ada types.

Pragma Convention C maybe applied to Ada types, but mostly has no effect, since this is the default. The following table shows the correspondence between Ada scalar types and the corresponding C types.

Integer
int
Short_Integer
short
Short_Short_Integer
signed char
Long_Integer
long
Long_Long_Integer
long long
Short_Float
float
Float
float
Long_Float
double
Long_Long_Float
This is the longest floating-point type supported by the hardware. Sometimes, this is the same as Long_Float, i.e. as the C type double. Otherwise, it is a wider type which is also available as long double in GNU C.

Interfacing to C++

The interface to C++ makes use of the following pragmas, which are primarily intended to be constructed automatically using a binding generator tool, although it is possible to construct them by hand. Ada Core Technologies does not currently supply a suitable binding generator tool.

Using these pragmas it is possible to achieve complete inter-operability between Ada tagged types and C class definitions. See section Implementation Defined Pragmas for more details.

pragma CPP_Class ([Entity =>] local_name)
The argument denotes an entity in the current declarative region that is declared as a tagged or untagged record type. It indicates that the type corresponds to an externally declared C++ class type, and is to be laid out the same way that C++ would lay out the type.
pragma CPP_Constructor ([Entity =>] local_name)
This pragma identifies an imported function (imported in the usual way with pragma Import) as corresponding to a C++ constructor.
pragma CPP_Vtable ...
One CPP_Vtable pragma can be present for each component of type CPP.Interfaces.Vtable_Ptr in a record to which pragma CPP_Class applies.

Interfacing to COBOL

Interfacing to COBOL is achieved as described in section B.4 of the Ada 95 reference manual.

Interfacing to Fortran

Interfacing to Fortran is achieved as described in section B.5 of the reference manual. The pragma Convention Fortran, applied to a multi- dimensional array causes the array to be stored in column-major order as required for convenient interface to Fortran.

Interfacing to non-GNAT Ada code

It is possible to specify the convention Ada in a pragma Import or pragma Export. However this refers to the calling conventions used by GNAT, which may or may not be similar enough to those used by some other Ada 83 or Ada 95 compiler to allow interoperation.

If arguments types are kept simple, and if the foreign compiler generally follows system calling conventions, then it may be possible to integrate files compiled by other Ada compilers, provided that the elaboration issues are adequately addressed (for example by eliminating the need for any load time elaboration).

In particular, GNAT running on VMS is designed to be highly compatible with the DEC Ada 83 compiler, so this is one case in which it is possible to import foreign units of this type, provided that the data items passed are restricted to simple scalar values or simple record types without variants, or simple array types with fixed bounds.


Go to the first, previous, next, last section, table of contents.