Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

GNAT, gprbuild and the Ada Toolchain

Set up a real Ada toolchain: GNAT, gprbuild project files, Alire, file naming rules, compiler switches and the compile-bind-link steps that turn Ada source into programs.

To run Ada for real you need a compiler. The most widely used one is GNAT, the Ada front end of the GNU Compiler Collection, available free from most Linux package managers, from AdaCore, and through the package manager Alire. This lesson explains how the pieces fit together so that you can build the read-only examples of this course on your own machine.

None of the commands below can run on this site, so they appear as read-only blocks. A C stand-in at the end models the three-stage build pipeline.

Getting GNAT

There are three common routes:

  1. Alire (recommended). Alire (alr) is the Ada package manager, like Cargo for Rust. It downloads a toolchain for you.
  2. Your OS package manager, for example sudo apt install gnat gprbuild on Debian and Ubuntu.
  3. GNAT Community or the AdaCore download pages, which provide installers for Windows and macOS.

Check the installation:

Terminal
gnatmake --version
gprbuild --version

The simplest build: gnatmake

Ada source files follow a strict naming rule, the GNAT default convention:

Unit File
Package specification Ada_Utils ada_utils.ads
Package body Ada_Utils ada_utils.adb
Procedure Hello hello.adb
Child package Parent.Child parent-child.ads

.ads stands for Ada specification and .adb for Ada body. The file name is the unit name in lowercase, with dots replaced by hyphens.

Save the Hello program from the previous lesson as hello.adb and run:

Terminal
gnatmake hello.adb
./hello

gnatmake works out which files are needed, compiles them in the right order, binds and links, and produces the executable hello.

Unlike C, Ada has a bind step. The binder checks that the whole program is consistent (every unit compiled against the same version of its dependencies), decides the elaboration order of packages, and generates the glue code that starts the program.

flowchart LR
    A[".adb / .ads files"] -->|"gcc -c"| B["object files + .ali info"]
    B -->|"gnatbind"| C["elaboration order + main wrapper"]
    C -->|"gnatlink"| D["executable"]

The .ali files record what each unit exposes and depends on; the binder reads them to reject inconsistent builds.

Project files with gprbuild

Larger programs use a project file named something like hello.gpr:

ADA
project Hello is
   for Source_Dirs use ("src");
   for Object_Dir  use "obj";
   for Exec_Dir    use "bin";
   for Main        use ("hello.adb");

   package Compiler is
      for Default_Switches ("Ada") use ("-gnat2022", "-gnata", "-gnatwa", "-O2");
   end Compiler;
end Hello;

Build with gprbuild -P hello.gpr. Useful compiler switches:

Switch Meaning
-gnat2022 Accept Ada 2022 syntax
-gnata Enable assertions and contract checks
-gnatwa Turn on most warnings
-gnato Overflow checking (on by default in modern GNAT)
-gnatp Suppress all checks (use with great care)
-g Debug information

Alire in thirty seconds

Terminal
alr init --bin my_app     # create a crate with a project file
cd my_app
alr build                 # fetch the toolchain if needed and build
alr run                   # run the program
alr with gnatcoll         # add a dependency from the Alire index

An Alire crate is a folder containing alire.toml, the .gpr file and a src folder. It is the easiest way to start real Ada projects and to manage libraries.

Reading compiler messages

GNAT's messages are quite readable, and each points at a line and column:

TEXT
hello.adb:5:04: error: "Put_Lin" is undefined
hello.adb:5:04: error: possible misspelling of "Put_Line"

GNAT also warns about unused variables, unreachable code and suspicious conversions. Treat warnings as errors in real projects with -gnatwe.

Stand-in: a three-stage build in C

This labelled C stand-in simulates what gnatmake does: compile every unit, bind them in dependency order, then link. It sorts units so that a unit is only "compiled" after the units it depends on, which is also what Ada's elaboration order demands.

C Stand-in: compile order and the binder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Dependencies are compiled first; if units depended on each other in a circle, the loop would report that no progress is possible. Ada also forbids such cycles between specifications.

A second stand-in shows the file-naming rule: it turns a unit name into the GNAT file name.

C Stand-in: unit name to file name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file name holds the body of a package named Stack_Utils under GNAT's default rules?

    The name is lowercase with the unit name unchanged (underscores stay), and .adb marks a body; .ads is for specifications.
  2. 2. What does the binder do that a C linker does not?

    The Ada binder verifies units were compiled against matching versions and generates the code that elaborates packages in a valid order.
  3. 3. Which tool is the Ada package manager?

    Alire (alr) manages crates and toolchains for Ada, like Cargo for Rust.
  4. 4. What does the -gnata switch do?

    -gnata turns on pragma Assert and Pre/Post aspects so they are checked at run time.

Summary

  • GNAT is the standard free Ada compiler; Alire installs it and manages libraries.
  • Files are name.ads (specification) and name.adb (body), with hyphens for child units.
  • A build is compile, bind, link; the binder enforces consistency and elaboration order.
  • Project files (.gpr) configure sources, switches and main programs for gprbuild.
  • Real Ada commands appear read-only here; C stand-ins model the pipeline.

Next: Variables, Constants and Declarations