int main = 0,已编译但崩溃

Man*_*lva -3 c c++ gcc g++

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

int main = 0;

此程序a.c与文件编译的罚款gcc-4.8gcc-5gcc-6gcc-7gcc-8和应用程序执行时坠毁。

a.cppg++-4.8和编译的文件相同的程序可以正常运行,g++-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;
     ^~~~
Run Code Online (Sandbox Code Playgroud)

注意:我已经遇到了这个问题,但是我没有回答我的问题。让我知道是否需要添加任何特定标签来引起对此特定问题有所了解的人的注意。

Vla*_*cow 7

具有这样的声明的程序

int main = 0;
Run Code Online (Sandbox Code Playgroud)

全局名称空间(或C中的文件范围)中的格式错误。

更现代的编译器可能会发出编译时错误。

来自C ++ 17 Standard(6.6.1主要功能)

  1. …在全局范围内声明变量main或使用C语言链接(在任何名称空间中)声明main名称的程序格式错误。名称main否则不保留。

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

namespace Name
{
    int main = 0;
}
Run Code Online (Sandbox Code Playgroud)