Compiling Programs (GNAT User’s Guide for Native Platforms)

#### 4.2.1 Compiling Programs ¶

The first step in creating an executable program is to compile the units of the program using the `gcc` command. You must compile the following files:

- the body file (`.adb`) for a library level subprogram or generic subprogram
- the spec file (`.ads`) for a library level package or generic package that has no body
- the body file (`.adb`) for a library level package or generic package that has a body

You need ‘not’ compile the following files

- the spec of a library unit which has a body
- subunits

because they are compiled as part of compiling related units. GNAT compiles package specs when the corresponding body is compiled, and subunits when the parent is compiled.

If you attempt to compile any of these files, you will get one of the following error messages (where `fff` is the name of the file you compiled):

    cannot generate code for file fff (package spec)

    cannot generate code for file fff (missing subunits)

    cannot generate code for file fff (subprogram spec)

    cannot generate code for file fff (subunit)

As indicated by the above error messages, if you want to submit one of these files to the compiler to check for correct semantics without generating code, then use the `-gnatc` switch.

The basic command for compiling a file containing an Ada unit is:

    $ gcc -c [switches] <file name>

where `file name` is the name of the Ada file (usually having an extension `.ads` for a spec or `.adb` for a body). You specify the `-c` switch to tell `gcc` to compile, but not link, the file. The result of a successful compilation is an object file, which has the same name as the source file but an extension of `.o` and an Ada Library Information (ALI) file, which also has the same name as the source file, but with `.ali` as the extension. GNAT creates these two output files in the current directory, but you may specify a source file in any directory using an absolute or relative path specification containing the directory information.

`gcc` is actually a driver program that looks at the extensions of the file arguments and loads the appropriate compiler. For example, the GNU C compiler is `cc1`, and the Ada compiler is `gnat1`. These programs are in directories known to the driver program (in some configurations via environment variables you set), but need not be in your path. The `gcc` driver also calls the assembler and any other utilities needed to complete the generation of the required object files.

It is possible to supply several file names on the same `gcc` command. This causes `gcc` to call the appropriate compiler for each file. For example, the following command lists two separate files to be compiled:

    $ gcc -c x.adb y.adb

calls `gnat1` (the Ada compiler) twice to compile `x.adb` and `y.adb`. The compiler generates two object files `x.o` and `y.o` and the two ALI files `x.ali` and `y.ali`.

Any switches apply to all the files listed, see Compiler Switches for a list of available `gcc` switches.