HP Fortran Programmer's Guide (B3908-90031; September 2011)

Compiling and linking
Special-purpose compilations
Chapter 2 93
Processing cpp directives
By default, the f90 command passes source files ending in the .F extension to cpp. Compiling with the
+cpp=yes option 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 # character, which is also
the prefix character for all cpp directives.)
Consider the following program:
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 #ifdef and #endif directives around PRINT statements. If the macro DEBUG is
defined, cpp will leave the PRINT statements 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 #define
directive; 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.
The following command line uses the -D option to define the macro DEBUG (the space between -D and
DEBUG is optional):
$ 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: