2.3 Running a Simple Ada Program

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 (for further information please 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 with 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 either gnatmake or gprbuild with the name of the main file: these tools are builders that will take care of all the necessary build steps in the correct order. In particular, these builders automatically recompile 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.