Next: Building Mixed Ada and C++ Programs, Previous: Interfacing to C, Up: Mixed Language Programming
GNAT follows standard calling sequence conventions and will thus interface to any other language that also follows these conventions. The following Convention identifiers are recognized by GNAT:
Note that in the case of GNAT running on a platform that supports HP Ada 83, a higher degree of compatibility can be guaranteed, and in particular records are laid out in an identical manner in the two compilers. Note also that if output from two different compilers is mixed, the program is responsible for dealing with elaboration issues. Probably the safest approach is to write the main program in the version of Ada other than GNAT, so that it takes care of its own elaboration requirements, and then call the GNAT-generated adainit procedure to ensure elaboration of the GNAT components. Consult the documentation of the other Ada compiler for further details on elaboration.
However, it is not possible to mix the tasking run time of GNAT and HP Ada 83, All the tasking operations must either be entirely within GNAT compiled sections of the program, or entirely within HP Ada 83 compiled sections of the program.
A note on interfacing to a C 'varargs' function:
In C, varargs allows a function to take a variable number of arguments. There is no direct equivalent in this to Ada. One approach that can be used is to create a C wrapper for each different profile and then interface to this C wrapper. For example, to print an int value using printf, create a C function printfi that takes two arguments, a pointer to a string and an int, and calls printf. Then in the Ada program, use pragma Import to interface to printfi.
It may work on some platforms to directly interface to a varargs function by providing a specific Ada profile for a particular call. However, this does not work on all platforms, since there is no guarantee that the calling sequence for a two argument normal C function is the same as for calling a varargs C function with the same two arguments.
function builtin_sqrt (F : Float) return Float; pragma Import (Intrinsic, builtin_sqrt, "__builtin_sqrtf");
Most of the GCC builtins are accessible this way, and as for other import conventions (e.g. C), it is the user's responsibility to ensure that the Ada subprogram profile matches the underlying builtin expectations.
GNAT additionally provides a useful pragma Convention_Identifier that can be used to parameterize conventions and allow additional synonyms to be specified. For example if you have legacy code in which the convention identifier Fortran77 was used for Fortran, you can use the configuration pragma:
pragma Convention_Identifier (Fortran77, Fortran);
And from now on the identifier Fortran77 may be used as a convention identifier (for example in an Import pragma) with the same meaning as Fortran.