Ada: First Steps
Recently, I have come to make my first steps in Ada. I have been reading the book Ada 2022 by John Barnes but had to figure out some details setting up an editor and a compiler chain.
Here, I share some practical tips how to get started.
Alire
Alire is a package manager provided by AdaCore. It comes as a script. Place it in any folder accessible from the PATH, e.g., ~/bin.
Afterwards, I add the path ~/.alire/bin to the PATH. For this, extend the file ~/.profile to contain
# .profile
if [ -d "$HOME/.alire/bin" ] ; then
PATH="$HOME/.alire/bin:$PATH"
fi
GNAT (GNU NYU Ada Translator) is an open-source implementation of an Ada compiler. Install GNAT using Alire:
alr install gnat gprbuild
Hello World
-- hello_world.adb
with Ada.Text_IO;
procedure Hello_World is
use Ada.Text_IO;
begin
Put_Line("Hello world.");
end Hello World;
GNU Make Workflow
What follows is a simple GNU Make-based workflow. Herein, GNAT’s manages all source dependencies. Accordingly, the $(MAIN) target does not name any dependencies.
In this example the dependency root is the procedure Hello_World stored in hello_world.adb.
# Makefile
AC := gnat
ACFLAGS := -g -gnatw.e
MAIN := hello_world
.PHONY: all
all:
$(AC) make $(ACFLAGS) $(MAIN)
.PHONY: clean
clean:
$(AC) clean $(MAIN)
With this Makefile in place, build the project like this:
make
GPRbuild Workflow
GPRbuild is a build system for Ada projects. It requires a project file describing the project.
Building an Application
-- src/hello_world.gpr
project Hello_World is
for Main use ("hello_world.adb");
for Source_Dirs use ("src");
for Object_Dir use "build";
package Compiler is
for Default_Switches ("Ada") use ("-g", "-gnatw.e");
end Compiler;
end Hello_World;
Building a Library
-- lib.gpr
project Lib is
for Library_Name use "lib";
for Library_Kind use "dynamic";
for Source_Dirs use ("src");
for Object_Dir use "build";
package Compiler is
for Default_Switches ("Ada") use ("-g", "-gnatw.e");
end Compiler;
end Lib;
With this project file in place, build the project like this:
gprbuild
IDE Setup
This section describes how to set up various IDEs. I have found, however, that I was unable to set up GNATbench.
Emacs Setup
Enable MELPA, then install the following package:
ada-ts-mode
Sublime Text Setup
Enable Package Control, then install the following package:
Ada
Visual Studio Code Setup
Here, I describe how to setup Visual Studio Code. In the Settings, set the following:
Chat,Miscellaneous: checkDisable AI Features.
Install the following extensions:
Ada & SPARK
Emacs Friendly Keymap
Open the project directory via File, Open Folder....
From the main menu select ..., Terminal, Configure Default Build Task.... Select ada: Build current project.
You can now build the project via ..., Terminal, Run Build Task....
You can run the project via Run, Run Without Debugging.
Debugging
Ensure, that the flag -g is enabled as an Ada compiler flag. Now, debug the executable the same way a C executable would.
gdb hello_world
Unit Testing with AUnit
See