Hello World in Ada
Your first Ada program - the classic Hello World example with Docker setup using GNAT
Every programming journey starts with Hello World. Let’s write our first Ada program using the GNAT compiler (GNU Ada).
The Code
Create a file named hello.adb:
| |
Understanding the Code
Ada programs are clean, readable, and structured. Let’s break down each part:
with Ada.Text_IO;- Imports the text input/output packageprocedure Hello is- Declares a procedure named Hello (the program entry point)begin- Marks the start of executable statementsAda.Text_IO.Put_Line ("Hello, World!");- Outputs text with a newlineend Hello;- Closes the procedure (must match the procedure name)
The With Clause
The with clause makes a package visible to your program. Ada.Text_IO is Ada’s standard I/O library, similar to stdio.h in C or iostream in C++. Unlike many languages, Ada requires explicit imports - there are no implicit global namespaces.
Procedure Declaration
Ada programs begin execution at a parameterless procedure. The structure is:
| |
Notice the semicolons - they terminate statements, not separate them. The end clause includes the procedure name, providing redundancy that helps catch errors.
Case Insensitivity
Ada is case-insensitive for keywords and identifiers. These are all equivalent:
| |
However, Ada style guides recommend:
- Keywords in lowercase:
with,procedure,begin,end - Package names in Mixed_Case:
Ada.Text_IO - Constants in ALL_CAPS:
MAX_SIZE - Variables in Mixed_Case:
Counter,Total_Amount
Running with Docker
The easiest way to run Ada without installing GNAT locally uses GCC (which includes GNAT) in a Docker container:
| |
Understanding the Docker Command
gnatmake- The GNAT build tool (handles compilation and linking)hello.adb- Input source file (.adb = Ada body)./hello- Run the compiled executable
The gnatmake command automatically:
- Compiles the source to object code
- Binds Ada-specific runtime information
- Links with the Ada runtime library
- Produces an executable
Running Locally
If you have GNAT installed:
| |
Installing GNAT
macOS (Homebrew):
| |
Ubuntu/Debian:
| |
Windows: Download from AdaCore Community Edition or use MSYS2:
| |
Fedora/RHEL:
| |
Expected Output
Hello, World!
Clean and simple - exactly what you’d expect!
Alternative Approaches
Ada’s standard library offers several ways to output text:
Using Use Clause for Brevity
| |
The use clause brings all of Ada.Text_IO into scope, so you can call Put_Line directly instead of Ada.Text_IO.Put_Line. This is more convenient but less explicit.
Using Put Instead of Put_Line
| |
Put outputs text without a newline. New_Line adds the newline explicitly.
With Declarations Section
| |
Variables and constants are declared between is and begin.
The .adb and .ads Files
Ada programs use two file types:
.adb (Ada Body)
Contains:
- Procedure/function implementations
- Main program code
- Package bodies
.ads (Ada Specification)
Contains:
- Package interfaces
- Type declarations
- Subprogram specifications
Our simple Hello World only needs a .adb file since it’s a standalone procedure. As programs grow, you’ll create packages with separate specification (.ads) and body (.adb) files.
Program Structure
A minimal Ada program requires:
- Optional with clauses - Import needed packages
- Procedure declaration - Define the entry point
- Optional declarations - Variables, constants, types
- Begin keyword - Start executable code
- Statements - Actual program logic
- End clause - Close the procedure
Example with Declarations
| |
Notice:
- Variables declared between
isandbegin &concatenates stringsInteger'Imageconverts integers to strings (Ada attributes use single quote)
Compilation Process
When you run gnatmake hello.adb, several things happen:
hello.adb → hello.ali (Ada Library Information)
→ hello.o (Object file)
→ hello (Executable)
The .ali Files
GNAT creates .ali (Ada Library Information) files containing:
- Dependency information
- Compilation options
- Source checksums
- Cross-reference data
These enable fast incremental compilation and consistency checking.
Separate Compilation Steps
You can also compile manually:
| |
Most developers use gnatmake or the more modern gprbuild tool.
Common Beginner Mistakes
Missing Semicolons
| |
Mismatched End Name
| |
Forgetting With Clause
| |
Wrong File Extension
Ada is case-insensitive, but file extensions matter:
hello.ada- ❌ Not recognized by GNAThello.adb- ✅ Correct for program bodyhello.ads- ✅ Correct for specification
Compiler Options
Useful GNAT compilation flags:
| |
Style Checking
| |
Why Ada for Hello World?
Even in this simple example, you see Ada’s philosophy:
- Explicit Imports - No hidden dependencies
- Clear Structure - Procedure declaration, begin/end blocks
- Readable Syntax - English-like keywords
- Name Redundancy -
end Hello;instead of justend - Package Qualification -
Ada.Text_IO.Put_Lineshows exactly where functions come from
These features become invaluable in large, safety-critical systems where Ada shines.
A Bit of History
Ada was named after Ada Lovelace (1815-1852), who wrote the first computer program - an algorithm for Charles Babbage’s Analytical Engine in 1843, nearly a century before electronic computers existed.
When the U.S. Department of Defense mandated Ada for most projects in 1987, Hello World programs compiled on room-sized VAX computers running VMS. Today, the same code compiles on a Raspberry Pi with GNAT - demonstrating Ada’s remarkable portability across four decades.
Performance Note
Ada’s strong typing and safety features might suggest overhead, but GNAT’s GCC backend produces highly optimized machine code. Ada programs run at C/C++ speeds - the safety checks happen at compile time, not runtime (except for explicit checks like array bounds, which can be disabled for production builds with -gnatp).
Next Steps
Continue to Variables and Data Types to learn about Ada’s powerful type system - the foundation of its safety guarantees.
Key Takeaways
- Ada requires explicit imports via
withclauses - Programs are procedures with
is/begin/endstructure - Semicolons terminate statements, not separate them
- End clauses include names for error checking
- GNAT is part of GCC and widely available
.adbfiles contain program bodies- Ada is case-insensitive but style guides recommend conventions
Running Today
All examples can be run using Docker:
docker pull codearchaeology/ada:latest