4 From Source Code to Executable


  <--Last Chapter Table of Contents Next Chapter-->  

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:

For example demo.adb would be an Ada program named demo.

NoteYou 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
  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
  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


(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:

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:
  1. 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.
  2. 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.
  3. 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:
  1. Compile the program with: gcc -c hello.adb
  2. Bind the program with: gnatbind hello.ali
  3. 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.

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:

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.

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.
NoteWhen 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:

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:

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]


  <--Last Chapter Table of Contents Next Chapter-->