gpt4 book ai didi

c++ - 使用 -std=c++0x 标志编译时,Unix 宏变得未定义

转载 作者:太空宇宙 更新时间:2023-11-04 14:39:00 26 4
gpt4 key购买 nike

当使用 -std=c++0x 标记编译下面的代码时,unix 宏变得未定义并且错误“Unix is not defined!”显示。发生这种情况有什么原因以及如何解决?在 gcc 版本 4.7.2 和 4.8.4 中验证。

#include <iostream>

#if !defined(unix)
#error Unix is not defined!
#endif

int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}

最佳答案

来自 GCC 手册,3.7.3 System-specific Predefined Macros :

The C standard requires that all system-specific macros be part of the reserved namespace. All names which begin with two underscores, or an underscore and a capital letter, are reserved for the compiler and library to use as they wish. However, historically system-specific macros have had names with no special prefix; for instance, it is common to find unix defined on Unix systems. For all such macros, GCC provides a parallel macro with two underscores added at the beginning and the end. If unix is defined, __unix__ will be defined too. There will never be more than two underscores; the parallel of _mips is __mips__.

When the -ansi option, or any -std option that requests strict conformance, is given to the compiler, all the system-specific predefined macros outside the reserved namespace are suppressed. The parallel macros, inside the reserved namespace, remain defined.

特别注意第二段。


tl;dr

unix 宏不符合标准,__unix__ 符合标准。当您向编译器询问 -std=c++0x 时,它切换到“严格一致性”,其中只有 __unix__ 可用(默认情况下支持“扩展” unix 已删除)。

关于c++ - 使用 -std=c++0x 标志编译时,Unix 宏变得未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48075584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com