识别操作系统 [英] Identify operating system
问题描述
Intel编译器上的Fortran 90代码取决于它所运行的操作系统,例如
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?
推荐答案
您可以使用预处理器指令执行此任务,请参阅这里和在这里查看详细信息:
You can use pre-processor directives for this task, see here and here for details:
-
_WIN32
li>
-
__ linux
for Linux -
__ APPLE __
对于Mac OSX
_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
确保启用预处理器通过指定 -fpp
/ / f pp
或在扩展名中给定文件大写 F
/ F90
。
您可以在中央位置进行此操作,确定例如一个描述操作系统的常量。这将避免这些宏在各地。
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.
请注意,没有针对Linux的宏由 gfortran
指定。由于它仍然在Windows上定义 _WIN32
,如果您只考虑Linux和Windows,则可以使用 #else
p>
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
这篇关于识别操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!