c++ - OSX 上的 GCC 5、6 和 7 不支持 uint

标签 c++ macos gcc

我使用 Homebrew 在 OSX 10.12 上安装了 GCC 5、6 和 7。编译简单代码

#include <iostream>

int main() {
    uint foo = 10;
    std::cout << foo << std::endl;
}

返回错误:

$ g++-7 -o uint uint.cpp
uint.cpp: In function 'int main()':
uint.cpp:5:5: error: 'uint' was not declared in this scope
     uint foo = 10;
     ^~~~
uint.cpp:5:5: note: suggested alternative: 'int'
     uint foo = 10;
     ^~~~
     int
uint.cpp:6:18: error: 'foo' was not declared in this scope
     std::cout << foo << std::endl;
                  ^~~
uint.cpp:6:18: note: suggested alternative: 'feof'
     std::cout << foo << std::endl;
                  ^~~
                  feof

我可以访问的其他编译器不会发生此错误。该代码适用于 clang++(在 OSX 上)和 Linux 系统上的 gcc4/5/6。 我这边缺少配置开关吗?或者这可能是因为 gcc 与 libstdc++ 链接而不是与 OSX 上的标准 libc++ 链接?

最佳答案

这应该是 GLIBC 的问题。参见 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59945以及 Jonathan Wakely 的回答。

Glibc 定义它:

#ifdef __USE_MISC
/* Old compatibility names for C types.  */
typedef unsigned long int ulong;
typedef unsigned short int ushort;
typedef unsigned int uint;
#endif

__USE_MISC 被定义是因为 G++ 定义了 _GNU_SOURCE,众所周知它会导致问题,例如PR 11196 和 PR 51749

这种特殊的命名空间污染只发生在 C++11 中,因为在 C++11 模式下只需要 #include 来定义 std::to_string、std::stoi 等,但通常这个问题也会影响 C++98 .

关于c++ - OSX 上的 GCC 5、6 和 7 不支持 uint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43892984/

相关文章:

C++ 二进制搜索算法不工作

android - 启动 Android 模拟器时出错

c++ - 尝试构建 C++ 程序时收到错误消息 "Template with C linkage"

c - 当我使用 -static 时 GCC 吓坏了

c++ - GCC 树 STL 数据容器

c++ - 在 Android NDK 中使用 C/C++ 加密(Rijndael 密码)

c++ - C 和 C++ 中的变长数组

c++ - 默认初始化整数成员是否首选空初始化器?

java - Spring log4j 无法在 osx 上保存日志文件

xcode - Safari 扩展伴侣始终为空