4 From Source Code to Executable
This section is an overview of creating new programs on Linux.
4.1 Gnat Filename Conventions
Unlike Microsoft Windows, Linux filenames do not require a suffix
to indicate the filetype. Nevertheless, Linux files often have
suffixes to make it easier to identify the type of files by their
names. gnat makes extensive use of suffixes. Here are some filename
conventions:
.ads
- Ada package specification
.adb
- Ada package body or program
.adc
- Gnat configuration file (for dead code elimination)
.adt
- Gnat tree file (for dead code elimination)
.adj
- Defaults for [NQS-KB]
.adp
- TIA project file or Gnat gnatxref/gnatfind project file
.cfg
- GLADE distributed program configuration
file
.ali
- debugging and linking information produced
by gnat
.xrb
- cross-reference file generated by
gnatf
GRASP_defaults...
- GRASP defaults file, holds
your preferences
.gpj
- a grasp project file
.gui
- a VAD TCL/TK widget description
For example, demo.adb
would be an Ada program named
demo
.
You can change the colour used by
ls to display these filenames by changing the
/etc/DIR_COLORS
file. Directions on how to do this are included in the file. |
4.2 Writing Your First Ada Program
4.2.1 Writing a program with an IDE:
Start a new project. For example, with TIA type
tia hello
to create a new project called "hello.adp". Type in the
following Ada program.
with text_io;
use text_io;
procedure hello is
begin
put_line( "Hello World!" );
end hello;
This Ada program will print a short message on the screen. Save
the file and build the project.
If there is a problem with your program, TIA will show you the
line number, the position on the line, and an error message
describing the problem. If you typed in the program correctly, you
should now have an executable file called hello in your directory.
TIA will ask you if you want to run your program.
Your program should display the message
Hello World!
before TIA's window reappears.
4.2.2 Writing a Program without an IDE
With a standard UNIX editor such as pico or vi, create the
following file called "hello.adb". For example,
pico hello.adb
Type in the following Ada program.
with text_io;
use text_io;
procedure hello is
begin
put_line( "Hello World!" );
end hello;
This Ada program will print a short message on the screen. When the
file is saved, the gnatmake command to build the project:
gnatmake hello.adb
If there is a problem with your program, gnatmake will show you
the line number, the position on the line, and an error message
describing the problem. If you typed in the program correctly, you
should now have an executable file called hello. Run the file by
typing
hello
(or ./hello on some distributions) and Linux will
respond by displaying the message
Hello World!
4.2.3 After Building
After building your project, there should be several files in your
directory:
hello.adb
- this is the Ada program you typed in.
The is called a source file.
hello.o
- this is the binary code created by the
compiler. This is called an object file.
hello.ali
- this is additional information about
the program created by gnat.
hello
- this is the executable program
If you want to clean up your directory, the hello.o and
hello.ali are information files and can be safely erased. However,
on large project with multiple files, leaving them will speed up
the building process.
4.3 The Three Step Process
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.
On simple projects, these steps can all be done automatically.
However, on some projects with particular requirements, you may
need to take special actions during one of these steps. 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".
4.4 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.
Many of the GCC switches listed in 4.5 can be used as well.
To use -gnatN or -gnatn:
- -O1 or better is required
- The subprogram that will be inlined must be small
- The subprogram must not have nested subprograms (gcc restriction)
- The subprogram must appear in a package body
4.4.1 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.
IDE: TIA sets these switches for you based on your
choices in the project parameters window. |
4.4.2 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.
IDE: TIA uses -gnatc when you chose File/Check. |
4.4.2 When you have Too Many Errors
When you have so many compiling errors that they run off the
top of the screen, you can redirect the errors to a file and list
them with the less command by adding the following to the end of
your compiling command: "2> temp.out; less temp.out".
4.5 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
When linking in libraries, the order of the
libraries is important. When libraries depend on each other,
libraries must be listed before the libraries that they use. |
4.7 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)
4.7.1 So you changed the comments...
Use gnatmake with the -m option, which updates gnat's
files without producing a new object code file. Use this to avoid
pointless recompilations when all you changed were the comments in
a source file.
4.7.2 Gnatbl: Bind and Link
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.
4.8 Project Management
In many cases, Gnat needs no "makefile" to build itself. The Ada source
code includes all the information about how the packages of a project are
related to one another. Recent versions of Gnat support project files (with
the gnatmake -P switch). Project files contain additional information about
how a gnatmake should build a project outside of the information contained in
the basic Ada source code.
The project files are laid out in an Ada style, using a "project" block:
project p [extends p2] is
...
Some clauses the appear are:
- for Main use unit;
- for Exec_Dir use path;
- for Source_Dirs use path-array;
- for Object_Dirs use path-array;
4.9 Linker Pragmas
In addition to the project file mentioned in 4.8, there are pragmas that
embed linking options into a source file so that they don't have to be listed
on the command line.
for Default_Switches( " ) embeds command line switches into
your source code. For example, for Default_Switches( "c" ) use ( "-mmcpu=pentium" );. The use portion is a list of comma-separated strings. Likewise,
use the langauage "ada" to add Ada switches: for Default_Switches( "ada" ) use ("-v"); --verbose.If the switch has double quotes, embed the quotes in
the string use two quotes or concatenate an ASCII.Quotation character.
[More to be written]