windows 在运行时确定操作系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8706958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine OS during runtime
提问by Philip
Neither ISO C nor POSIX offer functionality to determine the underlying OS during runtime. From a theoretical point of view, it doesn't matter since C offers wrappers for the most common system calls, and from a nit-picking point of view, there doesn't even have to bean underlying OS.
ISO C 和 POSIX 都不提供在运行时确定底层操作系统的功能。从理论的角度来看,它并不重要因为C语言提供包装为最常见的系统调用,并从一个吹毛求疵一点,有甚至没有要成为一个底层的操作系统。
However, in many real-world scenarios, it has proven helpful to know more about the host environment than C is willing to share, e.g. in order to find out where to store config files or how to call select()
, so:
然而,在许多实际场景中,事实证明,了解比 C 愿意分享的更多的主机环境是有帮助的,例如,为了找出存储配置文件的位置或如何调用select()
,因此:
Is there an idiomatic way for an application written in C to determine the underlying OS during runtime?
用 C 编写的应用程序是否有一种惯用的方法来确定运行时的底层操作系统?
At least, can I easily decide between Linux, Windows, BSD and MacOS?
至少,我可以轻松地在 Linux、Windows、BSD 和 MacOS 之间做出选择吗?
My current guess is to check for the existence of certain files/directories, such as C:\
or /
, but this approach seems unreliable. Maybe querying a series of such sources may help to establish the notion of "OS fingerprints", thus increasing reliability. Anyway, I'm looking forward to your suggestions.
我目前的猜测是检查某些文件/目录是否存在,例如C:\
or /
,但这种方法似乎不可靠。也许查询一系列这样的来源可能有助于建立“操作系统指纹”的概念,从而提高可靠性。无论如何,我期待着您的建议。
采纳答案by Tom van der Woerdt
Actually, most systems have a uname
command which shows the current kernel in use. On Mac OS, this is usually "Darwin", on Linux it's just plain "Linux", on Windows it's "ERROR" and FreeBSD will return "FreeBSD".
实际上,大多数系统都有一个uname
显示当前正在使用的内核的命令。在 Mac OS 上,这通常是“Darwin”,在 Linux 上它只是简单的“Linux”,在 Windows 上它是“ERROR”,而 FreeBSD 将返回“FreeBSD”。
More complete list of uname
outputs
I'm pretty sure that there's a C equivalent for uname
, so you won't need system()
我很确定有一个 C 等价的 for uname
,所以你不需要system()
回答by rejj
IFyou are on a POSIX system, you can call uname() from <sys/utsname.h>
.
如果您使用的是 POSIX 系统,则可以从<sys/utsname.h>
.
This obviously isn't 100% portable, but I don't think there will be any method that can grant that at runtime.
这显然不是 100% 可移植的,但我认为没有任何方法可以在运行时授予它。
see the man pagefor details
有关详细信息,请参阅手册页
回答by richo
Runtime isn't the time to determine this, being that without epic kludges binaries for one platform won't run on another, you should just use #ifdef
s around the platform sensitive code.
运行时不是确定这一点的时候,因为如果没有一个平台的史诗 kludges 二进制文件将不会在另一个平台上运行,您应该只#ifdef
在平台敏感代码周围使用s。
回答by gsamaras
The accepted answer states uname
, but doesn't provide a minimal working example, so here it is for anyone interested-hope it will save you the time it took for me:
接受的答案指出uname
,但没有提供最小的工作示例,因此这里适合任何感兴趣的人 - 希望它可以为您节省我花费的时间:
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>
int main(void) {
struct utsname buffer;
if (uname(&buffer) != 0) {
perror("uname");
exit(0);
}
printf("OS: %s\n", buffer.sysname);
return 0;
}
(Possible) Output:
(可能)输出:
OS: Linux
操作系统:Linux
PS: Unfortunately, this uses a POSIX header: Compilation fails due to missing file sys/utsname.h, which most probably won't work in Windows.
PS:不幸的是,这使用了 POSIX 标头:由于缺少文件 sys/utsname.h,编译失败,这很可能在 Windows 中不起作用。
回答by Roberto Cabellon
if (strchr(getenv("PATH"),'\'))
puts("You may be on windows...");
Even do I agree that "Runtime isn't the time to determine this..."
我什至同意“运行时不是确定这一点的时候......”