scope - Mechanics Fortran Preprocessor -
i happened come across preprocessing option fortran compilers support these days (as explained e.g. in fortran wiki) . coming c background, better understand mechanics , caveats related (fortran-)preprocessor's #include
directive.
to avoid confusion right beginning: there 2 include
directives in fortran (see e.g. f77 reference)
include "foo"
compiler directive, i.e. foo can contain fortran statements#include "bar"
preprocessor directive, i.e. bar can contain#defines
, like
i aware of difference , interested in second case (my question therefore not duplicate of this post).
i'll explain questions using example: assume have 2 files, header file (macro.h) , source file (display.f):
macro.h
#define msg() say_hello()
display.f
#include "macro.h" program display call msg() call another_message() end subroutine say_hello() write(*,*) 'hello.' end subroutine another_message() call msg() end
here questions:
scope
where (globally, locally in subroutine
etc.) macro msg()
defined if include macro.h:
- at beginning of file (as above)?
- at beginning of
program display
(and else)? - at beginning of e.g.
subroutine another_message()
(and else)?
from testing seems: 1. globally, 2. in program
, subroutines
, 3. in subroutine
only. confirmation of these assumptions , theoretical explanations why great.
what of above (1. - 3.) best practice preprocessor includes?
include guards
if have multi-file project , include header.h in multiple *.f source files, need provide include guards?
in case answers above questions should compiler dependent (as preprocessing not fortran standard), i'd interested in ifort's behaviour.
the rules same c preprocessor know. gcc uses same cpp
c , fortran (for fortran in traditional mode). therefore there no scope around, text , preprocessor doesn't care program units.
therefore, 1., 2. , 3. valid place of definition until file end or until #undef
. valid in recursively #include
d files.
if guards mean #undef
yes, otherwise warning or error redefinition appears, if include files single file. if independent no.
the key think preprocessor text replacement tool. knows nothing fortran.
last thing, preprocessor non-standard, available.
Comments
Post a Comment