首页 > 解决方案 > 确定用户是使用 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 语句中的那些逻辑条件。

标签: gfortranintel-fortran

解决方案


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

对于 gfortran,您可以按照此处__GNUC__所述查看。

对于ifort,你可以看这里__INTEL_COMPILER描述的。

如果你有文件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

推荐阅读