Identify operating system

£可爱£侵袭症+ 提交于 2019-12-12 17:38:33

问题


My Fortran 90 code on Intel compiler depends on the operating system it is running on, e.g.

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

How do I do this programmatically?


回答1:


You can use pre-processor directives for this task, see here and here for details:

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

Here is an example:

program test

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

end program

Make sure you enable the pre-processor by either specifying -fpp//fpp or given the file a capital F/F90 in the extension. You could do this in a central location, do define e.g. a constant describing the OS. This would avoid these Macros all over the place.

Please note that no macro for Linux is specified by gfortran. As it still defines _WIN32 on Windows, you can alternatively use #else if you just consider Linux and Windows:

program test

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

end program


来源:https://stackoverflow.com/questions/33813632/identify-operating-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!