2015-11-19 166 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中調用它。 –