Next: , Previous: Using an Ada Library, Up: GNAT and Libraries

18.4 Creating an Ada Library to be Used in a Non-Ada Context

The previous sections detailed how to create and install a library that was usable from an Ada main program. Using this library in a non-Ada context is not possible, because the elaboration of the library is automatically done as part of the main program elaboration.

GNAT also provides the ability to build libraries that can be used both in an Ada and non-Ada context. This section describes how to build such a library, and then how to use it from a C program. The method for interfacing with the library from other languages such as Fortran for instance remains the same.

18.4.1 Creating the Library

18.4.2 Using the Library

Libraries built as explained above can be used from any program, provided that the elaboration procedures (named mylibinit in the previous example) are called before the library services are used. Any number of libraries can be used simultaneously, as long as the elaboration procedure of each library is called.

Below is an example of C program that uses our mylib library.

     #include "mylib_interface.h"
     main (void)
        /* First, elaborate the library before using it */
        mylibinit ();
        /* Main program, using the library exported entities */
        do_something ();
        do_something_else ();
        /* Library finalization at the end of the program */
        mylibfinal ();
        return 0;

Note that this same library can be used from an equivalent Ada main program. In addition, if the libraries are installed as detailed in Installing an Ada Library, it is not necessary to invoke the library elaboration and finalization routines. The binder will ensure that this is done as part of the main program elaboration and finalization phases.

18.4.3 The Finalization Phase

Invoking any library finalization procedure generated by gnatbind shuts down the Ada run time permanently. Consequently, the finalization of all Ada libraries must be performed at the end of the program. No call to these libraries nor the Ada run time should be made past the finalization phase.

18.4.4 Restrictions in Libraries

The pragmas listed below should be used with caution inside libraries, as they can create incompatibilities with other Ada libraries:

When using a library that contains such pragmas, the user must make sure that all libraries use the same pragmas with the same values. Otherwise, a Program_Error will be raised during the elaboration of the conflicting libraries. The usage of these pragmas and its consequences for the user should therefore be well documented.

Similarly, the traceback in exception occurrences mechanism should be enabled or disabled in a consistent manner across all libraries. Otherwise, a Program_Error will be raised during the elaboration of the conflicting libraries.

If the 'Version and 'Body_Version attributes are used inside a library, then it is necessary to perform a gnatbind step that mentions all ALI files in all libraries, so that version identifiers can be properly computed. In practice these attributes are rarely used, so this is unlikely to be a consideration.