gcc - 为什么 gcc 不会编译未初始化的全局常量?

标签 gcc g++ global-variables

当我尝试使用 g++ 编译以下内容时:

const int zero;

int main()
{
  return 0;
}

我收到关于 uninitialized const 'zero' 的错误消息.我认为全局变量默认初始化为 0 [1] ?为什么这里不是这种情况?
VS 编译得很好。

[1] 例如,见 https://stackoverflow.com/a/10927293/331785

最佳答案

我的 gcc 稍微详细一点:

$ g++ zeroconst.c
zeroconst.c:1:11: error: uninitialized const ‘zero’ [-fpermissive]

我们看到 -fpermissive选项将允许编译。

uninitialized const 上看到这个问题引用 C++ 标准(问题是 C++ 特定的)。

GCC wiki 所引用:

As mandated by the C++ standard (8.5 [decl.init], para 9 in C++03, para 6 in C++0x), G++ does not allows objects of const-qualified type to be default initialized unless the type has a user-declared default constructor. Code that fails to compile can be fixed by providing an initializer...

关于gcc - 为什么 gcc 不会编译未初始化的全局常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14568830/

相关文章:

c - 警告 : incompatible implicit declaration of built-in function ‘xyz’

c - 标准输入 C : Incorrect string if $ is present

STL - C++ "no matching function for call to"结构错误

asp-classic - 如何在Classic ASP中使变量成为静态变量(或 “global”)?

c++ - 如何定义多个源文件访问的linux内核变量?

c++ - 使用 g++ 进行奇怪的零初始化

c - 为什么 GCC/Clang 的清除属性不能与函数参数一起使用

c++ - gcc 预编译头文件 : pragma once in main file

c++ - 使用G++的模板的编译错误,使用llvm编译正常

python - 类中的全局变量共享给其他实例