Next: Running a Program with Multiple Units, Previous: Running GNAT, Up: Getting Started with GNAT
Any text editor may be used to prepare an Ada program.
(If Emacs
is
used, the optional Ada mode may be helpful in laying out the program.)
The
program text is a normal text file. We will assume in our initial
example that you have used your editor to prepare the following
standard format text file:
with Ada.Text_IO; use Ada.Text_IO; procedure Hello is begin Put_Line ("Hello WORLD!"); end Hello; |
This file should be named hello.adb.
With the normal default file naming conventions, GNAT requires
that each file
contain a single compilation unit whose file name is the
unit name,
with periods replaced by hyphens; the
extension is ads for a
spec and adb for a body.
You can override this default file naming convention by use of the
special pragma Source_File_Name
(see Using Other File Names).
Alternatively, if you want to rename your files according to this default
convention, which is probably more convenient if you will be using GNAT
for all your compilations, then the gnatchop
utility
can be used to generate correctly-named source files
(see Renaming Files Using gnatchop).
You can compile the program using the following command ($
is used
as the command prompt in the examples in this document):
$ gcc -c hello.adb
gcc is the command used to run the compiler. This compiler is capable of compiling programs in several languages, including Ada and C. It assumes that you have given it an Ada program if the file extension is either .ads or .adb, and it will then call the GNAT compiler to compile the specified file.
The -c switch is required. It tells gcc to only do a compilation. (For C programs, gcc can also do linking, but this capability is not used directly for Ada programs, so the -c switch must always be present.)
This compile command generates a file
hello.o, which is the object
file corresponding to your Ada program. It also generates
an “Ada Library Information” file hello.ali,
which contains additional information used to check
that an Ada program is consistent.
To build an executable file,
use gnatbind
to bind the program
and gnatlink to link it. The
argument to both gnatbind
and gnatlink is the name of the
ALI file, but the default extension of .ali can
be omitted. This means that in the most common case, the argument
is simply the name of the main program:
$ gnatbind hello $ gnatlink hello
A simpler method of carrying out these steps is to use gnatmake, a master program that invokes all the required compilation, binding and linking tools in the correct order. In particular, gnatmake automatically recompiles any sources that have been modified since they were last compiled, or sources that depend on such modified sources, so that “version skew” is avoided.
$ gnatmake hello.adb
The result is an executable program called hello, which can be run by entering:
$ hello
assuming that the current directory is on the search path for executable programs.
and, if all has gone well, you will see
Hello WORLD!
appear in response to this command.