5

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 !

4
  • add_compile_options(-cpp) ? Commented Jun 4, 2019 at 16:35
  • You can provide target-specific compile options using target_compile_options(target PUBLIC -cpp). Commented Jun 4, 2019 at 16:40
  • Ok thank you, so a priori I still need to hard-code it. I'm going to use the one with target_ to stay "target centered". Commented Jun 5, 2019 at 13:17
  • I would caution you that there are certain obscure compilers for which -cpp is not the correct flag. I recommend checking if the compiler is intel before setting the flag, or using the try_compile command to set it only if it works. Commented Jun 5, 2019 at 23:04

1 Answer 1

3

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
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.