cmakefortranprecompiler

use cmake add_definition value in fortran


I'd like to include a flag such as -DMY_FLAG=ONETWOTHREE in the CMake call, i.e. make .. -DMY_FLAG=ONETWOTHREE, and get the value of MY_FLAG in the fortran code. I'm using add_definitions("-DMY_FLAG=${MY_FLAG}") to pass MY_FLAG to make.

Currently, when I do something like
write(*,*) MY_FLAG

I get this compiler error:
Error: Symbol 'ONETWOTHREE' at (1) has no IMPLICIT type

Can the -D flags be cast to a type in fortran? It looks like MY_FLAG is somehow defined at compile time, but it has no type.


Solution

  • When using a preprocessor, the final source file must be valid Fortran. In the present situation,

    write(*,*) ONETWOTHREE
    

    is not valid because there is not variable named ONETWOTHREE.

    Solutions:

    1. Define the variable earlier:

      integer ONETWOTHREE
      ONETWOTHREE = 5
      
    2. Do not use ONETWOTHREE but an actual value. Example for an integer:

      -DMY_FLAG=123
      

      so that the corresponding line will be

      write(*,*) 123
      

    Of course, it would be useful if you could provide us with the intention behind the usage of the preprocessor here.