Consider the following directory tree:
test.F90
test/
├─ a.inc
└─ b.inc
With the following file contents:
test.F90
:
#include "test/a.inc"
end
a.inc
:
#if defined(__GFORTRAN__) | defined(__PGI) | defined(__NVCOMPILER)
# include "b.inc"
#else
# include "test/b.inc"
#endif
#include "b.inc"
#include "b.inc"
b.inc
:
print*,'!'
The GNU (gfortran 11.1.0) and Nvidia (nvfortran 22.9) compiler preprocessors search for the file to be included in the directory relative to the current file. E.g. b.inc
should be included inside of a.inc
with #include "b.inc"
.
The Intel (ifort 2021.8.0, ifx 2023.0.0) and NAG (nagfor 7.1) compiler preprocessors search for the file to be included in the directory relative to the first file to invoke #include
. E.g. b.inc
should be included inside of a.inc
with #include "test/b.inc"
because a.inc
is included in test.F90
. However, it only applies for the first #include
command. In all consequent #include
commands, the directory is "changed" to test
and #include "b.inc"
can be used. While multiple #include "test/b.inc"
commands also work in the Intel compiler, the code doesn't compile with the NAG compiler if the consequent paths are changed to test/b.inc
.
Is this the intended behavior of the Intel and NAG compiler preprocessors? Is there a way to make the behavior of the #include
command independent of the used compiler (and avoid the first 5 lines in a.inc
)?