ZetCode

Ada project files and gprbuild

last modified November 23, 2026

Every article so far has built a program with gnatmake and a growing list of switches and file names. A real project needs more: where the sources live, where the objects and executables go, which files are main programs, which compiler options apply, and which libraries the program uses. GPR project files describe all of this in one place, and gprbuild reads them. This article builds a first project, adds a second main program, introduces build modes and switches, packages code as a static library, links a client against it, and looks at how Alire manages projects.

The first project file

A project file is a small declarative file with the extension .gpr. In its simplest form it names the directory that holds the Ada sources, the directory for objects, the directory for executables, and the main source file. The main file must contain the main procedure and its name must match the unit name, just as it does for gnatmake. The program in this first project is deliberately small so that the project file stays in focus.

src/hello.adb
with Ada.Text_IO; use Ada.Text_IO;

procedure Hello is
begin
   Put_Line ("Hello there from a project!");
end Hello;

The project file below introduces four attributes. Source_Dirs lists the directories searched for sources, Object_Dir is where the compiler writes its .o and .ali files, Exec_Dir is where the linked program goes, and Main names the main source file. Create_Missing_Dirs lets gprbuild create the output directories on the first build instead of failing because they do not exist.

hello.gpr
project Hello is
   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Exec_Dir use "bin";
   for Main use ("hello.adb");
   for Create_Missing_Dirs use "True";
end Hello;

gprbuild -P hello.gpr builds the project named on the command line. The output lists each phase: creating the directories, compiling the Ada source, binding, and linking. The executable is written to bin/hello, and running it prints the message. The command gprclean -P hello.gpr removes the objects and the executable again, leaving the sources untouched.

$ gprbuild -P hello.gpr
Setup
   [mkdir]        object directory for project Hello
   [mkdir]        exec directory for project Hello
Compile
   [Ada]          hello.adb
Bind
   [gprbind]      hello.bexch
   [Ada]          hello.ali
Link
   [link]         hello.adb
$ ./bin/hello
Hello there from a project!

A second main program

One project can produce several executables. Adding a name to Main tells gprbuild to bind and link another main program, and every source file that any of the mains depends on is compiled once and shared. The next program prints three short lines, so its output is easy to tell apart from the first one.

src/greet.adb
with Ada.Text_IO; use Ada.Text_IO;

procedure Greet is
begin
   for I in 1 .. 3 loop
      Put_Line ("greeting" & Integer'Image (I));
   end loop;
end Greet;

The only change to the project file is the list of mains. hello.adb has not changed, so the second build compiles only greet.adb, then binds and links both programs. Each main gets its own binder file and its own executable in bin.

hello.gpr
project Hello is
   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Exec_Dir use "bin";
   for Main use ("hello.adb", "greet.adb");
   for Create_Missing_Dirs use "True";
end Hello;

The build output shows that hello.adb is not compiled a second time. The two executables are independent programs, so neither depends on the other at run time; they share only the object files of the units they both use.

$ gprbuild -P hello.gpr
Compile
   [Ada]          greet.adb
Bind
   [gprbind]      hello.bexch
   [Ada]          hello.ali
   [gprbind]      greet.bexch
   [Ada]          greet.ali
Link
   [link]         hello.adb
   [link]         greet.adb
$ ./bin/greet
greeting 1
greeting 2
greeting 3

Build modes and compiler switches

Compiler switches belong in the project file too. They are set inside a package Compiler block, and the attribute Default_Switches ("Ada") applies them to every Ada compilation in the project. A common arrangement is a debug build with symbolic information and all warnings, and a release build with optimisation. A project variable selects between them.

hello.gpr
project Hello is
   type Mode_Type is ("debug", "release");
   Mode : Mode_Type := external ("MODE", "debug");

   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Exec_Dir use "bin";
   for Main use ("hello.adb", "greet.adb");
   for Create_Missing_Dirs use "True";

   package Compiler is
      case Mode is
         when "debug" =>
            for Default_Switches ("Ada")
              use ("-g", "-gnat2022", "-gnatwa");
         when "release" =>
            for Default_Switches ("Ada")
              use ("-O2", "-gnat2022", "-gnatwa");
      end case;
   end Compiler;
end Hello;

external ("MODE", "debug") reads the value of the environment variable or command-line switch MODE and falls back to debug when it is not set. The command line supplies it as -XMODE=release. The case statement then chooses one set of switches. -g keeps debugging information, -O2 optimises the generated code, and both modes keep -gnat2022 and the warning set -gnatwa.

The first command below forces a debug build with -f, and the second forces a release build. Forcing is needed here because the two switch sets differ but the sources do not; without -f, gprbuild may decide that the existing objects are up to date. Both builds produce the same program output, since the mode affects code generation and diagnostics, not the algorithm.

$ gprbuild -P hello.gpr -f
Compile
   [Ada]          hello.adb
   [Ada]          greet.adb
Bind
   [gprbind]      hello.bexch
   [Ada]          hello.ali
   [gprbind]      greet.bexch
   [Ada]          greet.ali
Link
   [link]         hello.adb
   [link]         greet.adb
$ gprbuild -P hello.gpr -XMODE=release -f
Compile
   [Ada]          hello.adb
   [Ada]          greet.adb
Bind
   [gprbind]      hello.bexch
   [Ada]          hello.ali
   [gprbind]      greet.bexch
   [Ada]          greet.ali
Link
   [link]         hello.adb
   [link]         greet.adb
$ ./bin/hello
Hello there from a project!

A package to share

A library begins as ordinary Ada packages. The example package computes the area of a circle and of a square, and its specification exposes only the two functions and the value of pi. The body keeps the arithmetic. Nothing in the package mentions a library yet; it compiles with gnatmake just like any other unit.

shapes/src/shapes.ads
package Shapes is
   Pi : constant := 3.14159;
   function Circle_Area (Radius : Float) return Float;
   function Square_Area (Side : Float) return Float;
end Shapes;

The body implements both functions with the usual arithmetic. The value of pi is a named number, so it is exact in the source and does not occupy storage. The two functions are primitives of no type, but they are ordinary library units that a library project can collect and export.

shapes/src/shapes.adb
package body Shapes is
   function Circle_Area (Radius : Float) return Float is
   begin
      return Pi * Radius * Radius;
   end Circle_Area;

   function Square_Area (Side : Float) return Float is
   begin
      return Side * Side;
   end Square_Area;
end Shapes;

A client that only needs the area of a circle can with Shapes; and call the function, without seeing the body or any of its dependencies. That separation is what makes the next two project files useful.

A library project

A library project compiles the same sources and archives the objects into a library file. Library_Name gives the archive its name, Library_Dir says where to put it, and Library_Kind selects a static or a shared library. A static library is simply an archive of object files that is copied into each executable that uses it.

shapes/shapes.gpr
project Shapes is
   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Library_Name use "shapes";
   for Library_Dir use "lib";
   for Library_Kind use "static";
   for Create_Missing_Dirs use "True";
end Shapes;

Building the project compiles shapes.adb and then runs the archiver and the indexer. The result is lib/libshapes.a, together with the ALI file that describes the library's interface. Library_Kind could be "dynamic" instead, which would produce a shared object, but the static form needs no search path at run time and keeps the examples simple.

$ gprbuild -P shapes/shapes.gpr
Setup
   [mkdir]        object directory for project Shapes
   [mkdir]        library directory for project Shapes
Compile
   [Ada]          shapes.adb
Build Libraries
   [gprlib]       shapes.lexch
   [archive]      libshapes.a
   [index]        libshapes.a
$ ls shapes/lib
libshapes.a
shapes.ali

A client project

A project that uses the library imports the library project with a with clause. This is a GPR with, not the Ada one: it makes the imported project's units visible to the compiler and lets gprbuild build the library first when necessary. Paths inside each project file are relative to that file's own directory, so the client's "src" means client/src.

client/src/client.adb
with Ada.Text_IO; use Ada.Text_IO;
with Shapes;

procedure Client is
   package Float_IO is new Ada.Text_IO.Float_IO (Float);
begin
   Put ("circle area = ");
   Float_IO.Put (Shapes.Circle_Area (2.0), Fore => 1, Aft => 2, Exp => 0);
   New_Line;
   Put ("square area = ");
   Float_IO.Put (Shapes.Square_Area (3.0), Fore => 1, Aft => 2, Exp => 0);
   New_Line;
end Client;

The project file names the library with a relative path and then declares the usual main program. gprbuild -P client/client.gpr sees the dependency and builds shapes.gpr first if its library is missing or out of date.

client/client.gpr
with "../shapes/shapes.gpr";

project Client is
   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Exec_Dir use "bin";
   for Main use ("client.adb");
   for Create_Missing_Dirs use "True";
end Client;

The client compiles and links against the archive in shapes/lib, and the resulting executable in client/bin runs without any special environment variable. The two areas in the output are computed by the library, and the formatting is done by the client.

$ gprbuild -P client/client.gpr
Setup
   [mkdir]        object directory for project Client
   [mkdir]        exec directory for project Client
Compile
   [Ada]          client.adb
Bind
   [gprbind]      client.bexch
   [Ada]          client.ali
Link
   [link]         client.adb
$ ./client/bin/client
circle area = 12.57
square area = 9.00

Alire in a nutshell

Alire is the package manager and build front end for Ada. A project managed by Alire has a manifest called alire.toml, which records the project's own version, its dependencies, and the compiler version it expects. The command alr init --bin demo creates a binary project with a demo.gpr file and a manifest, and alr build fetches the dependencies, sets up the environment, and then calls gprbuild with the right project file. The GPR files in this article are exactly what Alire generates and builds; the manifest only adds the package information around them.

Alire is not required to use gprbuild, and this machine does not have the alr executable installed, so the examples here were built directly with gprbuild. A project that uses only the standard library needs nothing more, and the same .gpr file can be used both with and without Alire.

Next steps

The series continues with unit testing, which builds a test suite for a small package and runs it from a project of its own. The environment and processes article explains the environment variables that external ("MODE", ...) reads.

Source

GPRbuild User's Guide

GNAT User's Guide

Alire documentation

In this article we have written a first project file, added a second main program, selected a debug or release build with an external variable, packaged two functions as a static library, linked a client against that library, and placed Alire in the picture. The packages article explains the specification and body that the library project compiles.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.