Next: , Up: Compiling Using gcc

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

You need not compile the following files

because they are compiled as part of compiling related units. GNAT 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)
     to check package spec, use -gnatc
     cannot generate code for file fff (missing subunits)
     to check parent unit, use -gnatc
     cannot generate code for file fff (subprogram spec)
     to check subprogram spec, use -gnatc
     cannot generate code for file fff (subunit)
     to check subunit, use -gnatc

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 three separate files to be compiled:

     $ gcc -c x.adb y.adb z.c

calls gnat1 (the Ada compiler) twice to compile x.adb and y.adb, and cc1 (the C compiler) once to compile z.c. The compiler generates three object files x.o, y.o and z.o and the two ALI files x.ali and y.ali from the Ada compilations. Any switches apply to all the files listed, except for -gnatx switches, which apply only to Ada compilations.