Previous: Use of Alternative Implementations, Up: Modeling Conditional Compilation in Ada


3.10.1.5 Preprocessing

Although it is quite possible to conditionalize code without the use of C-style preprocessing, as described earlier in this section, it is nevertheless convenient in some cases to use the C approach. Moreover, older Ada compilers have often provided some preprocessing capability, so legacy code may depend on this approach, even though it is not standard.

To accommodate such use, GNAT provides a preprocessor (modeled to a large extent on the various preprocessors that have been used with legacy code on other compilers, to enable easier transition).

The preprocessor may be used in two separate modes. It can be used quite separately from the compiler, to generate a separate output source file that is then fed to the compiler as a separate step. This is the gnatprep utility, whose use is fully described in Preprocessing with gnatprep.

The preprocessing language allows such constructs as

    #if DEBUG or else (PRIORITY > 4) then
       bunch of declarations
    #else
       completely different bunch of declarations
    #end if;

The values of the symbols DEBUG and PRIORITY can be defined either on the command line or in a separate file.

The other way of running the preprocessor is even closer to the C style and often more convenient. In this approach the preprocessing is integrated into the compilation process. The compiler is fed the preprocessor input which includes #if lines etc, and then the compiler carries out the preprocessing internally and processes the resulting output. For more details on this approach, see Integrated Preprocessing.