确定用户是使用 gfortran 还是 ifort

问题描述

我正在用现代 Fortran 编写代码,我想做一些类似的事情:

IF (you are compiling the code with gfortran) Do something...
IF (you are compiling the code with ifort) Do other thing...

但我还没有找到一种方法来验证代码中 IF 语句中的那些逻辑条件。

解决方法

最简单的方法是查看特定于编译器的宏。

对于 gfortran,您可以按照 here 中的说明查看 __GNUC__

为了方便,您可以查看 __INTEL_COMPILER,如here 所述。

如果您有文件 test.F90(注意 .F90 而不是 .f90,预处理此文件很重要),那么您可以拥有类似

program test
  implicit none
  
#ifdef __GNUC__
  logical,parameter :: gfortran = .true.
#else
  logical,parameter :: gfortran = .false.
#endif
  
#ifdef __INTEL_COMPILER
  logical,parameter :: ifort = .true.
#else
  logical,parameter :: ifort = .false.
#endif
  
  if (gfortran) then
    write(*,*) 'gfortran'
  elseif (ifort) then
    write(*,*) 'ifort'
  else
    write(*,*) 'Unknown compiler'
  endif
end program