int main = 0,已编译但崩溃了

问题描述 投票:-1回答:4

我有一个非常简单的程序,如下所示。

int main = 0;

此程序作为a.c文件,用gcc-4.8gcc-5gcc-6gcc-7gcc-8编译正确,执行时应用程序崩溃。

a.cpp文件相同的程序,用g++-4.8g++-5编译良好,执行时应用程序崩溃。但是在更高版本的g ++中,它会给出编译时错误。

下面是我的问题。

  1. 为什么a.cpp可以在g ++-4.8和5上很好地编译,而在以后的版本中却不能?它是在g ++的先前版本中出错还是在c ++标准中有所改进?
  2. 如果编译成功,那么为什么应用程序崩溃?

下面是详细信息的编译器和编译/执行的输出。

+ 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,但是我没有回答我的问题。让我知道是否需要添加任何特定标签来引起对此特定问题有所了解的人的注意。

c++ c gcc g++
4个回答
7
投票

具有这样的声明的程序

int main = 0;

在全局名称空间(或C中的文件范围)中,格式不正确。

[更多现代编译器会发出编译时错误。

从C ++ 17 Standard(6.6.1主要功能)

  1. …一个程序,它在全局范围内声明变量main或声明C语言链接(在任何命名空间中)的主要名称为格式错误。主名称没有其他保留。

例如,您可以通过以下方式在C ++程序中使用这样的声明

namespace Name
{
    int main = 0;
}

1
投票

对编译器之间的更改的部分解释是,在C ++标准之间,对main的约束已更改。

C ++ 98(第3.6节第3段)说

在程序中不得使用功能main(3.2)。 main的链接(3.5)是实现定义的。声明maininlinestatic的程序格式错误。主名称没有其他保留。

[C ++ 11基本上说了同样的话-只是它删除了对3.2节的引用,并且也不允许mainconstexpr(与C ++ 11引入constexpr的事实一致。]]]

[因此,我建议-在C ++ 11之前-编译器实现者对于如果代码在名为main的文件作用域中定义了一个变量,该怎么做有很大的自由度。实际上,我倾向于进一步建议他们可能根本没有考虑过这种特殊情况,但是-如果在程序编译和崩溃时进行查询-他们可能会声称行为不确定。当标准完全不限制发生的情况时,会发生一种类型的未定义行为。

C ++ 17,将对main函数的讨论移至第6.6.1节。在该部分的第3段中说(以粗体突出显示我的文字,而不是较早的标准中的文字)

main功能不得在程序内使用。 main的链接(6.5)是实现定义的。将main定义为已删除或将main声明为inlinestaticconstexpr的程序格式错误。 main函数不得以链接规范(10.5)声明。 声明变量的程序全局范围内的main或声明与C语言链接(在任何命名空间中)的名称main的格式不正确。

名称main否则未保留。

从C ++ 17开始,在文件范围内在文件范围内定义名为main的变量会导致程序格式错误-这意味着实现可能会诊断错误。


0
投票

对于C


0
投票
这里有两个稍微不同的问题:
© www.soinside.com 2019 - 2024. All rights reserved.