The easiest way to build a library is to use the Project Manager, which supports a special type of project called a ‘Library Project’ (see the ‘Library Projects’ section in the ‘GNAT Project Manager’ chapter of the GPRbuild User’s Guide).
A project is considered a library project when two project-level
attributes are defined in it: Library_Name
and Library_Dir
. In
order to control different aspects of library configuration, you can
specify additional optional project-level attributes:
Library_Kind
This attribute controls whether the library is to be static or dynamic
Library_Version
This attribute specifies the library version. Its value is used during dynamic linking of shared libraries to determine if the currently installed versions of the binaries are compatible.
Library_Options
Library_GCC
These attributes specify additional low-level options to be used during library generation and the commands used to generate the library.
The GNAT Project Manager takes complete care of the library maintenance task,
including recompilation of the source files for which objects do not exist
or are not up to date, assembly of the library archive, and installation of
the library (i.e., copying associated source, object and ALI
files
to the specified location).
Here’s a simple library project file:
project My_Lib is for Source_Dirs use ("src1", "src2"); for Object_Dir use "obj"; for Library_Name use "mylib"; for Library_Dir use "lib"; for Library_Kind use "dynamic"; end My_lib;
and the compilation command to build and install the library:
$ gnatmake -Pmy_lib
It’s complex to manually perform all the steps required to produce a library, so we recommend you use the GNAT Project Manager for this task. In case this is not desired, we discuss the necessary steps below.
There are various possibilities for compiling the units that make up
the library: for example with a Makefile
(Using the GNU make Utility) or with a conventional script. For
simple libraries, you can also create a dummy main program
that depends upon all the packages that comprise the interface of the
library. You can then pass this dummy main program to gnatmake
,
which will ensure all necessary objects are built.
After the above has been accomplished, you should follow the standard procedure of the underlying operating system to produce the static or shared library.
Here’s an example of such a dummy program:
with My_Lib.Service1; with My_Lib.Service2; with My_Lib.Service3; procedure My_Lib_Dummy is begin null; end;
Here are the generic commands that will build an archive or a shared library.
# compiling the library $ gnatmake -c my_lib_dummy.adb # we don't need the dummy object itself $ rm my_lib_dummy.o my_lib_dummy.ali # create an archive with the remaining objects $ ar rc libmy_lib.a *.o # some systems may require "ranlib" to be run as well # or create a shared library $ gcc -shared -o libmy_lib.so *.o # some systems may require the code to have been compiled with -fPIC # remove the object files that are now in the library $ rm *.o # Make the ALI files read-only so that gnatmake will not try to # regenerate the objects that are in the library $ chmod -w *.ali
Please note that the library must have a name of the form lib`xxx'.a
or lib`xxx'.so
(or lib`xxx'.dll
on Windows) in order to
be accessed by the -l`xxx'
switch at link time.