如何找到操作系统的名称? [英] How do I find the name of an operating system?

查看:134
本文介绍了如何找到操作系统的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单.我想要一个函数(C ++)或方法,该函数或方法将在调用时恢复

The questions pretty simple. I want want a function (C++) or method which will, on call, returun something like

"Windows" //or
"Unix"

没什么,我不需要版本numbe或任何东西.只是操作系统名称.快速的Google searc并没有显示出任何有用的信息,因此我认为应该在此处发布

Nothing fancy, I dont need the version numbe or anything. Just the os name. A quick google searc didnt turn up anything useful, so I thought I'd post this here

推荐答案

由于您无法在所有操作系统上运行一个二进制文件,因此您需要重新重新编译代码.可以使用MACRO.

Since you can not have a single binary file which runs over all operating systems, and you need to re-compile your code again. It's OK to use MACROs.

使用诸如

_WIN32
_WIN64
__unix
__unix__
__APPLE__
__MACH__
__linux__
__FreeBSD__

像这样

std::string getOsName()
{
    #ifdef _WIN32
    return "Windows 32-bit";
    #elif _WIN64
    return "Windows 64-bit";
    #elif __APPLE__ || __MACH__
    return "Mac OSX";
    #elif __linux__
    return "Linux";
    #elif __FreeBSD__
    return "FreeBSD";
    #elif __unix || __unix__
    return "Unix";
    #else
    return "Other";
    #endif
}                      

您应该阅读编译器手册,并查看它们提供了哪些MACROS来在编译时检测操作系统.

这篇关于如何找到操作系统的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆