2015-11-19 169 views
1

英特尔编译器上的我的Fortran 90代码取决于它运行的操作系统,例如,识别操作系统

if (OS=="win7") then 
    do X 
else if (OS=="linux") then 
    do y 
end if 

如何以编程方式执行此操作?

+2

如果你需要一个运行时检查,你可以使用'get_environment_variable(“PATH”,pathstring)',然后检查'pathstring'以'/'开头。 – agentp

回答

2

您可以使用预处理器指令完成这个任务,看到herehere的细节:

  • _WIN32为Windows
  • __linux为Linux
  • __APPLE__为Mac OSX

这里是一个例子:

program test 

#ifdef _WIN32 
    print *,'Windows' 
#endif 
#ifdef __linux 
    print *,'Linux' 
#endif 

end program 

确保您通过指定-fpp//fpp或扩展给出的文件中的资本F/F90使预处理器。 你可以在中央位置做这件事,确定例如一个描述操作系统的常量。这将避免这些宏在各地。

请注意,gfortran没有指定用于Linux的宏。因为它仍然在Windows上定义_WIN32,您也可以使用#else如果你只是考虑使用Linux和Windows:

program test 

#ifdef _WIN32 
    print *,'Windows' 
#else 
    print *,'Linux' 
#endif 

end program 
+1

去此路线http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system可能会有用。也是我认为,而不是预处理Fortran我会写一个确定操作系统的C函数,使用预处理来获得所需的信息,因为这些宏是为这些宏设计的,然后从Fortran中调用它。 –