【问题标题】:GCC 5, 6 and 7 on OSX do not support uintOSX 上的 GCC 5、6 和 7 不支持 uint
【发布时间】:2017-05-10 12:48:04
【问题描述】:

我使用 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 上)和 gcc4/5/6 在 Linux 系统上。 我这边是否缺少配置开关?或者这可能是因为 gcc 与 libstdc++ 链接而不是与 OSX 标准的 libc++ 链接?

【问题讨论】:

  • uint 不是关键字,stdint.h 没有声明它。你认为它应该来自哪里?
  • 你认为uint 是什么? uintN_tuint_leastN_tuint_fastN_t 形式中有类型,但不仅仅是 uint 本身。
  • 好吧,我可以做一个typedef uint unsigned int,但这并不能回答我的问题。
  • 您要查找的标志是-Duint="unsigned int"。我当然是在开玩笑……但它确实有效。
  • 你好像忘记了#include "uint.h"

标签: c++ macos gcc


【解决方案1】:

这应该是 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 .

【讨论】:

  • 谢谢!确实很有趣。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多