Chapter 1. Compiling with GCC

Table of Contents
1.1. Compiling Programs
1.2. Switches for GCC
1.3. Search Paths and the Run-Time Library
1.4. Order of Compilation Issues
1.5. Examples

This chapter discusses how to compile Ada programs using the gcc command, and the command line switches that can be used to control the behavior of the Ada compiler.

1.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. XGC Ada compiles generic units when a client instantiates the generic, 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 messages (where Filename is the name of the file you compiled):


No code generated for file Filename (package spec)
No code generated for file Filename (subunit)

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


$ prefix-gcc -c [switches] filename

where filename 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. XGC Ada 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, 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-code 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:


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

calls gnat1 (the Ada 95 compiler) twice to compile x.adb and y.adb, and cc1 (the ANSI 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.