HP Fortran Programmer Guide (766160-001, March 2014)

cppdirectives are similar to debugging lines, a feature of many Fortran implementations see, “Using
debugging lines” (page 80). Like cppdirectives, debugging lines enable the compiler to treat
source lines as either compilable statements or comments to be removed before compilation. But
debugging lines are nonstandard, available only in fixed-form source, and not nearly as powerful
as the cpp directives. Although cppdirectives are not a standard feature of Fortran, cppis a de
facto standard feature of UNIX systems.
This section discusses how to do the following:
Invoke cpp from the f90command line.
Use the -D option to define cppmacros.
Save the preprocessed output generated by cpp.
For more information about the cppcommand and the directives it supports, see the cpp (1) man
page.
Processing cpp directives
By default, the f90 command passes source files ending in the .Fextension to cpp. Compiling
with the +cpp=yesoption enables you to override this default and cause the f90 driver to pass
all source files to cpp. If you do not compile with the +cpp=yes option and if the source file does
not have the .F extension, the compiler treats any cpp directives (but not any embedded Fortran
statements) as comments and ignores them. (As a language extension, HP Fortran allows comments
to begin with the , which is also the prefix character for all cpp directives.)
Consider the following program:
Example 13 Example 2-9 cpp_direct.f90
PROGRAM main
REAL :: x
WRITE (6, FMT=(A), ADVANCE=NO) Enter a real number:
READ *, x
#ifdef DEBUG
PRINT *, The value of x in main: , x
#endif
PRINT *, x =, double_it(x)
END PROGRAM main
REAL FUNCTION double_it(arg)
REAL :: arg
#ifdef DEBUG
PRINT *, The value of x in double_it: , arg
#endif
double_it = 2.0 * arg
END FUNCTION double_it
The program uses the #ifdefand #endifdirectives around PRINTstatements. If the macro DEBUG
is defined, cppwill leave the PRINTstatements in the source text that is passed to the compiler; if
it is not defined, cpp will remove the statements. You can define the macro in the source text, using
the #definedirective; or you can define it on the command line, using the -D command-line
option. The advantage of the option is that it does not require editing the source file to define or
undefine a macro.
$ f90 +cpp=yes -D DEBUG cpp_direct.f90
Here is the output from a sample run of the executable program created by the preceding command
line:
$ a.out
Enter a real number: 3
The value of x in main: 3.0
Using the C preprocessor 61