Remy F
Remy F

Reputation: 1529

Can cmake automatically add fortran preprocessor -cpp flag?

I have some fortran sources with preprocessor instructions. I usually compile them with ifort -cpp -DOPT1 -DOPT2 ... in a Makefile. Now I'm trying to change to cmake, and to add these options I use target_compile_definitions(target PUBLIC OPT1). But when I run the compilation, the command shows only ifort -DOPT1 -DOPT2 ... and therefore fails.

I've read that some compilers have this flag set by default, like gfortran:

-cpp
-nocpp  Enable preprocessing. The preprocessor is automatically invoked if
        the file extension is .fpp, .FPP, .F, .FOR, .FTN, .F90, .F95, .F03
        or .F08. Use this option to manually enable preprocessing of any 
        kind of Fortran file.

but it looks like this is not explicitly the case for ifort:

-cpp   Runs the Fortran preprocessor on source files prior
       to compilation (same as the -fpp option).

Is there an option I missed in cmake to add this flag or not ? If no, what would be the proper way to add it manually with a cmake command ?

Thank you !

Upvotes: 5

Views: 1421

Answers (1)

Mike T
Mike T

Reputation: 43642

If you have CMake 3.18 or later, use the Fortran_PREPROCESS property:

set_source_files_properties(
  file1.fpp file2.fpp
  PROPERTIES Fortran_PREPROCESS ON
)

Upvotes: 3

Related Questions