我使用 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/