Running a Simple Ada Program (GNAT User’s Guide for Native Platforms)

### 2.3 Running a Simple Ada Program ¶

You may use any text editor to prepare an Ada program. (If you use Emacs, an 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 named `hello.adb`:

    with Ada.Text_IO; use Ada.Text_IO;
    procedure Hello is
    begin
       Put_Line ("Hello WORLD!");
    end Hello;

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 you use can use the `gnatchop` utility 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. It is capable of compiling programs in several languages, including Ada and C. It assumes you have given it an Ada program if the file extension is either `.ads` or `.adb`, in which case it will 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 you must always specify the `-c`.)

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 perform 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, as well as 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 you can 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.