Generating Object Files (GNAT User’s Guide for Native Platforms)

### 3.5 Generating Object Files ¶

An Ada program consists of a set of source files and the first step in compiling the program is generating the corresponding object files. You generate these by compiling a subset of these source files. The files you need to compile are the following:

- If a package spec has no body, compile the package spec to produce the object file for the package.
- If a package has both a spec and a body, compile the body to produce the object file for the package. You need not compile the source file for the package spec in this case because there’s only one object file, which contains the code for both the spec and body of the package.
- For a subprogram, compile the subprogram body to produce the object file for the subprogram. You need not compile the spec, if such a file is present.

- In the case of subunits, only compile the parent unit. GNAT generates a single object file for the entire subunit tree, which includes all the subunits.
- Compile child units independently of their parent units (though, of course, the spec of all the ancestor unit must be present in order to compile a child unit).
- Compile generic units in the same manner as any other units. The object files in this case are small dummy files that contain, at most, the flag used for elaboration checking. This is because GNAT always handles generic instantiation by means of macro expansion. However, you still must compile generic units for dependency checking and elaboration purposes.

The preceding rules describe the set of files that must be compiled to generate all the object files for a program. See the following section on dependencies for more details on computing that set of files. Each object file has the same name as the corresponding source file, except that the extension is `.o`, as usual.

You may wish to compile other files for the purpose of checking their syntactic and semantic correctness. For example, in the case where a package has a separate spec and body, you would not normally compile the spec. However, it is convenient in practice to compile the spec to make sure it is error-free before compiling clients of this spec because such compilations will fail if there is an error in the spec.

GNAT provides an option for compiling such files purely for the purposes of checking correctness; such compilations are not required as part of the process of building a program. To compile a file in this checking mode, use the `-gnatc` switch.