我有一个非常简单的程序,如下所示。
int main = 0;
此程序作为a.c
文件,用gcc-4.8
,gcc-5
,gcc-6
,gcc-7
和gcc-8
编译正确,执行时应用程序崩溃。
与a.cpp
文件相同的程序,用g++-4.8
和g++-5
编译良好,执行时应用程序崩溃。但是在更高版本的g ++中,它会给出编译时错误。
下面是我的问题。
下面是详细信息的编译器和编译/执行的输出。
+ gcc-4.8 --version
gcc-4.8 (Ubuntu 4.8.5-4ubuntu8) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ gcc-4.8 c.c
+ ./a.out
Segmentation fault (core dumped)
+ gcc-5 --version
gcc-5 (Ubuntu 5.5.0-12ubuntu1) 5.5.0 20171010
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ gcc-5 c.c
+ ./a.out
Segmentation fault (core dumped)
+ gcc-6 --version
gcc-6 (Ubuntu 6.5.0-2ubuntu1~18.04) 6.5.0 20181026
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ gcc-6 c.c
+ ./a.out
Segmentation fault (core dumped)
+ gcc-7 --version
gcc-7 (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ gcc-7 c.c
+ ./a.out
Segmentation fault (core dumped)
+ gcc-8 --version
gcc-8 (Ubuntu 8.3.0-6ubuntu1~18.04.1) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ gcc-8 c.c
+ ./a.out
Segmentation fault (core dumped)
+ g++-4.8 --version
g++-4.8 (Ubuntu 4.8.5-4ubuntu8) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ g++-4.8 c.cpp
+ ./a.out
Segmentation fault (core dumped)
+ g++-5 --version
g++-5 (Ubuntu 5.5.0-12ubuntu1) 5.5.0 20171010
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ g++-5 c.cpp
+ ./a.out
Segmentation fault (core dumped)
+ g++-6 --version
g++-6 (Ubuntu 6.5.0-2ubuntu1~18.04) 6.5.0 20181026
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ g++-6 c.cpp
c.cpp:1:5: error: cannot declare ‘::main’ to be a global variable
int main = 0;
^~~~
+ g++-7 --version
g++-7 (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ g++-7 c.cpp
c.cpp:1:5: error: cannot declare ‘::main’ to be a global variable
int main = 0;
^~~~
+ g++-8 --version
g++-8 (Ubuntu 8.3.0-6ubuntu1~18.04.1) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ g++-8 c.cpp
c.cpp:1:5: error: cannot declare ‘::main’ to be a global variable
int main = 0;
^~~~
注意:我已经遇到this,但是我没有回答我的问题。让我知道是否需要添加任何特定标签来引起对此特定问题有所了解的人的注意。
具有这样的声明的程序
int main = 0;
在全局名称空间(或C中的文件范围)中,格式不正确。
[更多现代编译器会发出编译时错误。
从C ++ 17 Standard(6.6.1主要功能)
- …一个程序,它在全局范围内声明变量main或声明C语言链接(在任何命名空间中)的主要名称为格式错误。主名称没有其他保留。
例如,您可以通过以下方式在C ++程序中使用这样的声明
namespace Name
{
int main = 0;
}
对编译器之间的更改的部分解释是,在C ++标准之间,对main
的约束已更改。
C ++ 98(第3.6节第3段)说
在程序中不得使用功能
main
(3.2)。main
的链接(3.5)是实现定义的。声明main
为inline
或static
的程序格式错误。主名称没有其他保留。
[C ++ 11基本上说了同样的话-只是它删除了对3.2节的引用,并且也不允许main
为constexpr
(与C ++ 11引入constexpr
的事实一致。]]]
[因此,我建议-在C ++ 11之前-编译器实现者对于如果代码在名为main
的文件作用域中定义了一个变量,该怎么做有很大的自由度。实际上,我倾向于进一步建议他们可能根本没有考虑过这种特殊情况,但是-如果在程序编译和崩溃时进行查询-他们可能会声称行为不确定。当标准完全不限制发生的情况时,会发生一种类型的未定义行为。
C ++ 17,将对main
函数的讨论移至第6.6.1节。在该部分的第3段中说(以粗体突出显示我的文字,而不是较早的标准中的文字)
名称
main
功能不得在程序内使用。main
的链接(6.5)是实现定义的。将main
定义为已删除或将main
声明为inline
,static
或constexpr
的程序格式错误。main
函数不得以链接规范(10.5)声明。 声明变量的程序全局范围内的main
或声明与C语言链接(在任何命名空间中)的名称main的格式不正确。main
否则未保留。从C ++ 17开始,在文件范围内在文件范围内定义名为
main
的变量会导致程序格式错误-这意味着实现可能会诊断错误。
对于C