1

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.

raf
  • 535
  • 4
  • 18

1 Answers1

3

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.

Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22
  • apparently cmake wants a specific syntax when using `add_definitions`. does NOT work: `add_definitions("-DMY_FLAG=${MY_FLAG}")`. does work: `add_definitions(-DMY_FLAG="${MY_FLAG}")`. This is for a string value for `MY_FLAG`. For a numeric value, no quotes are necessary. – raf Aug 15 '17 at 13:47
  • This is in line with the [documentation](https://cmake.org/cmake/help/latest/command/add_definitions.html): overall quoting inside `add_definitions` is not to be used. The quotes are for the replacement in the source code as strings are quoted in Fortran. – Pierre de Buyl Aug 16 '17 at 07:30