在 C++ 中确定是 Linux 还是 Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3213037/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:20:19  来源:igfitidea点击:

Determine if Linux or Windows in C++

c++windowslinuxcross-platform

提问by Elpezmuerto

I am writing a cross-platform compatible function in C++ that creates directories based on input filenames. I need to know if the machine is Linux or windows and use the appropriate forward or back slash. For the following code below, if the machine is Linux then isLinux = true. How do I determine the OS?

我正在用 C++ 编写一个跨平台兼容函数,它根据输入文件名创建目录。我需要知道机器是 Linux 还是 windows 并使用适当的正斜杠或反斜杠。对于下面的代码,如果机器是 Linux 则isLinux = true. 如何确定操作系统?

bool isLinux;
std::string slash;
std::string directoryName;

if isLinux
   slash = "/";
else
   slash = "\";
end

boost::filesystem::create_directory (full_path.native_directory_string() + slash + directoryName); 

回答by Artyom

Use:

用:

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
static const std::string slash="\";
#else
static const std::string slash="/";
#endif

BTW, you can still safely use this slash "/" on Windows as windows understands this perfectly. So just sticking with "/" slash would solve problems for all OSes even like OpenVMS where path is foo:[bar.bee]test.extcan be represented as /foo/bar/bee/test.ext.

顺便说一句,您仍然可以安全地在 Windows 上使用这个斜杠“/”,因为 Windows 完全理解这一点。因此,坚持使用“/”斜线可以解决所有操作系统的问题,甚至像 OpenVMS 一样,其中路径foo:[bar.bee]test.ext可以表示为/foo/bar/bee/test.ext.

回答by Billy ONeal

Generally speaking, you'd have do do this with conditional compilation.

一般来说,您必须使用条件编译来完成此操作。

That said, if you're using boost::filesystemyou should be using the portable generic path formatso that you can forget about things like this.

也就是说,如果你正在使用boost::filesystem你应该使用可移植的通用路径格式,这样你就可以忘记这样的事情。

回答by bobobobo

By default, Visual Studio #defines _WIN32in the preprocessor project settings.

默认情况下,Visual Studio中#defineŞ_WIN32预处理器中的项目设置。

So you can use

所以你可以使用

// _WIN32 = we're in windows
#ifdef _WIN32
// Windows
#else
// Not windows
#endif

回答by Fanatic23

Look into http://en.wikipedia.org/wiki/Uname

查看http://en.wikipedia.org/wiki/Uname

If you are using g++ as your compiler/GNU then you could try the code below. POSIX compliant environments support this:

如果您使用 g++ 作为编译器/GNU,那么您可以尝试下面的代码。POSIX 兼容环境支持:

#include <stdio.h>
#include <sys/utsname.h>
#include <stdlib.h>

int main()
{
    struct utsname sysinfo;
    if(uname(&sysinfo)) exit(9);
    printf("os name: %s\n", sysinfo.sysname);
    return 0;
}

回答by nathan

One of the most used methods to do this is with a pre-processor directive. The link is for C but they're used the same way in C++. Fair warning, each compiler & OS can have their own set of directives.

最常用的方法之一是使用预处理器指令。该链接适用于 C,但它们在 C++ 中的使用方式相同。公平警告,每个编译器和操作系统都可以有自己的一组指令。

回答by Ronny Brendel

predef.sourceforge.netis a comprehensive collection of all kinds of MACROs that identify compilers/operating systems and more. (linking directly to the operating system sub-site)

predef.sourceforge.net是各种MACRO的综合集合,用于识别编译器/操作系统等。(直接链接到操作系统子站点)