I know how to use IF
and ENDIF
in Fortran, but in many codes I have from others, there is a #
sign before the IF
, what does that mean?
!USES:
#if ( defined X )
use Y
#endif
I know how to use IF
and ENDIF
in Fortran, but in many codes I have from others, there is a #
sign before the IF
, what does that mean?
!USES:
#if ( defined X )
use Y
#endif
These are pre-compiler macros, and not Fortran statements. They do the same as regular if
statements, only at compile time and with a slightly different syntax.
Note that pre-processor directives are not part of any Fortran Standard, and that they are not supported by every compiler. Generally, they are identical to C pre-processor directives, but not always.
suppose you had a file:
! test.F:
program A
x=1
#ifdef NEW
x=0
#endif
write(*,*) x
end
if you compile it using a preprocessor:
ifort -DNEW test.F -o a.out
you should get x =0
otherwise if you compile without -DNEW like
ifort test.F -o a.out
in that case you get x=1. The code inside the ifdef is not compiled. Here are compile time options for gfortran.. A useful situation for example is this question.