Previous: Installing a library, Up: General Ada Libraries


3.9.2.3 Using a library

Once again, the project facility greatly simplifies the use of libraries. In this context, using a library is just a matter of adding a `with' clause in the user project. For instance, to make use of the library My_Lib shown in examples in earlier sections, you can write:

    with "my_lib";
    project My_Proj is
      ...
    end My_Proj;

Even if you have a third-party, non-Ada library, you can still use GNAT's Project Manager facility to provide a wrapper for it. For example, the following project, when `with'ed by your main project, will link with the third-party library liba.a:

    project Liba is
       for Externally_Built use "true";
       for Source_Files use ();
       for Library_Dir use "lib";
       for Library_Name use "a";
       for Library_Kind use "static";
    end Liba;

This is an alternative to the use of pragma Linker_Options. It is especially interesting in the context of systems with several interdependent static libraries where finding a proper linker order is not easy and best be left to the tools having visibility over project dependence information.

In order to use an Ada library manually, you need to make sure that this library is on both your source and object path (see Search Paths and the Run-Time Library (RTL) and Search Paths for gnatbind). Furthermore, when the objects are grouped in an archive or a shared library, you need to specify the desired library at link time.

For example, you can use the library mylib installed in /dir/my_lib_src and /dir/my_lib_obj with the following commands:

    $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \\
      -largs -lmy_lib

This can be expressed more simply:

    $ gnatmake my_appl

when the following conditions are met:

Note that you may also load a library dynamically at run time given its filename, as illustrated in the GNAT plugins example in the directory share/examples/gnat/plugins within the GNAT install area.