1.1. 🦿 Compile and Run - JulTob/Ada GitHub Wiki

🦾 Compilation

To compile the program with GNAT compiler you should follow the file and name convention it follows.

For instance, a compilable unit has the extension .adb (Ada Body).

The name of the file has to be the one of the main procedure in your code, in all lower case.

As such, this file should be named:

hello.ada

with Ada.Text_IO;    --Input Output Standard Library

procedure Hello is
begin 
   --  Print "Hello,  Ada!" to the screen
   Ada.Text_IO.Put_Line(“Hello, Ada!”);
end Hello;

The specification files are given the extension “.ads”.

To compile hello.adb open a console or terminal window and use the following command

gnat make hello.adb

This will produce an executable file (hello.exe on Windows).

Gnat comes with a graphical Ada Programming Environment named GPS (Gnat Programming Studio).

When the compilation has completed successfully you will find that several additional files have been created. The files hello.o and hello.exe(on Windows) are the object file and executable file respectively. The file hello.ali is the Ada library information file. This file is used by GNAT to implement some of the consistency checking required by the Ada language. It is a plain text file; feel free to look at it. However, you would normally ignore the ali files. If you delete them, they will simply be regenerated the next time you compile your program

🦾 To compile the file into an executable binary.

gnatmake -g hello_world.adb

🕸 The option -g produces debug information by the compiler. It generates the files b~hello.adb(ads,ali,o) with bug 🐛 information.

💪🏼 Also the files generated

  • hello.o is the binary object
  • hello.ali is the linker
  • hello Is the executable.

💪🏽 The only “important” file is the last one, that can execute the program. You can run it as ./hello. It displays the string “Hello, Ada!” in the console. Without the quotation marks.

👉🏼 You can also run it at: Online Compiler

🦿 Main Program (?)

🦿 The main program is the procedure named Hello. But this is incidental. The precise name used for the main program can be any word. Exactly which procedure is used as the program’s entry point is specified when the program is compiled by the name of the source code file.

👩🏻‍🌾 If the name is count_sheeps.adbit will start executing the procedure count_sheeps in the file.

👩🏻‍🎨 Case INsensitive

It means that
cat,Cat,CAT,cAt... are all the same object.

If you come from most other programming languages that’s a bit surprising. But it eliminates a lot of errors in programming, as it is more natural, closer to how language works. Instead, in other languages cat is an object, Cat is the class (an abstract platonic perfect cat), CAT is a precompiled constant variable, and other combinations are code you just can’t understand (80% of the time, just an acronym only Satan knows)

This also forces creativity and produces explanatory power to your code.

👩🏻‍🦲 Empty statement.

An empty statement cannot be a hanging semicolon. You have to be explicit with a codeword null

null;

Compilation

To compile the program with GNAT compiler you should follow the file and name convention it follows. For instance, a compilable body unit has the extension .adb (Ada Body). The name of the file has to be the one of the main procedure in your code, in all lower case. As such, this file should be named:

hello.adb The specification files are given the extension .ads. To compile hello.adb open a console or terminal window and use the following command

gnatmake hello.adb

This will produce an executable file (hello.exe on Windows).

Gnat comes with a graphical Ada Programming Environment named GPS (Gnat Programming Studio).

When the compilation has completed successfully you will find that several additional files have been created.

The files hello.o and hello.exe(on Windows) are the object file and executable file respectively.

The file hello.ali is the Ada library information file. This file is used by GNAT to implement some of the consistency checking required by the Ada language. It is a plain text file; feel free to look at it. However, you would normally ignore the ali files. If you delete them, they will simply be regenerated the next time you compile your program

gprbuild hello.adb

To compile units independently:

gnat make <module>

To see all gnat commands:

gnat 

for help:

gnat <command>

When you build a project using gnatmake, or when you use an IDE to run gnatmake for you, gnatmake performs three operations:

**Compiling**: gnatmake checks your file for errors. If there are no errors, it creates an object file containing the binary version of your program. If there are any errors in your file, gnatmake stops.
**Binding**: gnatmake verifies that all the files in the project are up to date. If there are files that need to be compiled, gnatmake will compile them as well.
**Linking**: gnatmake combines all the object files to create an executable program.

You can perform these separate steps yourself. For example, using the hello.adb program:

    Compile the program with: gcc -c hello.adb
    Bind the program with: gnatbind hello.ali
    Link the program with: gnatlink hello.ali

Once again, you have an executable program called "hello".

Gnat Compiling Options The version of gcc for gnat has all of the normally document gcc switches, plus some new switches for gnat. You can run gcc by itself, or have gnatmake run gcc for you. Unless otherwise noted, these switches can be applied to both gcc and gnatmake.

-b - For crosscompiling, compile for a target machine
-Bdir Multiple gnats, load the gnat compiler from directory dir instead of the default one
-c - gcc only, tells gcc to compile only and not to try to link with the C linker
-g - create an executable that can be used with the gdb debugger
-Idir - Beside the directory with the first file, check directory dir for more source files
-I- - Do not look for source files in the directory where the first file resides
-On -On Optimize, from 0 (none) to 3 (maximum, automatic internal inlining). See below.
-s - gcc only, create an assembly language source file instead of an object file
-Wuninitialized - warnings on uninitialized variables
-v - show what steps the gcc compiler is performing
-gnata - turn on debugging pragmas. See below.
-gnatb - keep messages brief
-gnatc - check the program, but don't compile it
-gnatdx - activate ACT internal debugging switch 'x', where x is a character
-gnatD - with -gnatG, save debugging info to files ending in .dx
-gnate - display error message immediately instead of waiting until end of compile (versions prior to 3.14)
-gnatE - turn on dynamic elaboration checks
-gnatf - give more information about errors
-gnatg - turn on gnat style checks
-gnatG - show pseudo-code of how Gnat interprets your source code (for internal use by GNAT)
-gnatic - use character set c
-gnatkn- constrain file names to n characters
-gnatl - include source code with error messages
-gnatL - C++ exception handling (setjmp/longjmp)
-gnatmn - show no more than n errors
-gnatn - allow inline subprograms across source code in different files
-gnatN - inline as much as possible, even subprograms not marked for inlining
-gnato - turn on checks normally turned off (such as numeric overflow checking)
-gnatp - turn all checks off
-gnatq - don't quit because of errors--compile entire source file
-gnatr - check for reference manual source code layout
-gnatR - listing with alignment info
-gnats - synatx check only
-gnatt - create tree output file
-gnatu - list units being compiled
-gnatVx - change level of validity checks to n (none), default (d), full (f).
-gnatwm - warning mode(s) m. These include
    -gnatwa - show all optional warnings
    -gnatwA - show no optional warnings
    -gnatwc - warnings for always true/false expressions in statements
    -gnatwe - treat warnings as errors
    -gnatws - suppress warnings
    -gnatwl - warnings on elaboration errors
    -gnatwu - warnings on unused variables, uninitialized parameters, unused packages
    -gnatyk - check indentifier case
-gnatx - suppress cross-reference information
-gnaty - Impose line length limit, etc. [? - KB]
-gnatzm - generate distribution stubs for m
-gnatZ - zero-cost exceptions (default)
-gnat83 - enforce old Ada 83 conventions
-gnat95 - enforce Ada 95 conventions (default)
-mno-486 - create an executable that can run on a Intel 386 or newer
-m486 - create an executable that can run on a Intel 486 or newer
-mcpu=model - compile an executable for the given cpu model
-fstack-check - required for checks for stack overflows. If you are experiencing core dumps on large arrays, try turning on this switch to raise a STORAGE_ERROR exception instead

The -gnat switches can be combined together, such as -gnatbcs for -gnatb, -gnatc, and -gnats.

If your operating system environment has a variable called ADA_OBJECTS_PATH, use this like to list directories containing Ada files built into operating system libraries (like .a or .so files). Use ADA_INCLUDE_PATH to specify directories containing .ads files for these system libraries. These can be combined with -L or -I switches: the variables and switches have the same function.

Run-time Error Checking

Ada has extensive checking for run-time errors. By default, gnat turns off some of these checks to improve the speed of the programs. To turn on all error checking, you need to use -gnato -gnatE switches. To turn off all error checking, you need to use -gnatp.

Checking without Compiling

In gnat 3.10, if you want to check a source file without actually compiling it, use the gnatf utility. In gnat 3.11 or later, you can use gcc with the -gnatc option to check a source file.

Gnat Binding Options gnatbind checks the integrity of a project before the linking phase. You can run gnatbind by itself, or have gnatmake run it for you.

-A - (default) generate binder program in Ada. See -C, -x.
-aIdir - besides the directory of the file, search for source files in directory dir
-aOdir - besides the directory of the file, search for .ali files in directory dir
-b - brief messages
-C - generate binder program (in C, not Ada). See -A, -x.
-c - check only
-e - list elaboration dependancies
-E - exception stack traceback (when compiling with -funwind-tables)
-f - use full reference manual semantics in an attempt to find a legal elaboration order
-h - help
-Idir -combination of -aI and -aO
-I- - don't look for source and .ali files in regular places
-l - list the chosen elaboration order
-mn - show no more than m binding errors
-Mn - main program to be called n, not the default name
-n - no main program, for when the main program is written in another language
-nostdinc - (no standard includes) ignore default directory when looking for sources
-nostdlib - (no standard libraries) ignore default directory when looking for libraries
-o file - output to a file name file
-O - list objects
-p - pessimistic - try worst case elaboration order
-r - renames the main program from main to gnat_main
-s - require all source files to be present
-shared - link in Gnat run-time library as a shared library (if available--for example, ALT version)
-static - link in Gnat run-time library statically
-t - ignore time stamp errors
-Tn - tasking time slices are n milliseconds long. n=0 means no time slicing, as per Annex D.
-we - treat warnings as errors
-ws - suppress all binder warnings
-v - verbose messages
-x - check only, ignore source files.  Don't generate a binder program.  See -s.
-z - same as -n [?-KB]

4.6 Gnat Linking Options gnatlink combines object files together to form a finished executable program. You can run it by itself, or gnatmake can run it for you.

-o file - name of executable file to create
-v - verbose messages
-gnatlink n instead of gcc, use linker named n
-l lib in the specified library

Gnatmake Options To compile any Ada program, use the gnatmake command. gnatmake checks all the packages a program relies upon and automatically compiles any packages that need compiling. For example,

gnatmake main.adb

will compile the file main.adb, automatically compiling all Ada files referenced by main.adb, if necessary. This is unlike other building tools like make and cook because the dependancy of source files is listed in every Ada file by the with statement. make and cook are designed to work with C which has no equivalent statement and requires the programmer to list the dependencies in a separate file.

When gnatmake is finished compiling, it will automatically bind and link the program, producing an executable file called main.

There are times when you want gnatmake to compile the project, but not to bind or link it. You can tell gnatmake not to link by using the -n option:

gnatmake -n main.adb

Here is a summary of the gnatmake switches:

-a - consider all files, even read-only source files and standard system files like ada.text_io
-c - compile only
-f - recompile entire project
-jn - on multiprocessor machiens, compile using n processes at once
-k - ignore errors, and compile as much as possible
-M - create a list of dependences suitable for make's Makefile
-i - instead of the current directory, keep intermediate files in directories where their sources are found
-m - update dependancies without compiling
-n - check dependencies, but don't do anything
-o name - save the executable as name
-q - quiet - no status messages
-s - switch change - recompile if the switches have changed
-u - compile only indicated file. Don't remake whole project.
-v - verbose - explain why files are compiled
-aIdir - besides the directory of the first file, search directory dir for source files
-aOdir - besides the directory of the first file, search directory dir for object and .ali files
-Adir - same as -aIdir and -aLdir
-Idir - same as -aodir -aIdir
-I- - don't look for source files in the directory of the first file
-Ldir - besides directory of the first file, look for libraries in directory dir
-cargs s - pass switches s to compiler
-bargs s - pass switches s to binder
-largs s - span>pass switches s to linker (e.g. -largs somefile.o to link in the somefile object file)

In certain cases, such as mixing different languages with Ada, you may need to compile the source files with different compilers but the binding and linking can be done in one step. gnatbl is a shortcut tool that runs gnatbind and gnatlink.

⚠️ **GitHub.com Fallback** ⚠️