gpt4 book ai didi

c++ - int main = 0,已编译但崩溃

转载 作者:行者123 更新时间:2023-11-30 20:44:41 24 4
gpt4 key购买 nike

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

int main = 0;

该程序作为 a.c 文件使用 gcc-4.8gcc-5gcc-6 编译良好、gcc-7gcc-8 以及应用程序在执行时崩溃。

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

以下是我的问题。

  1. 为什么 a.cpp 在 g++-4.8 和 5 中编译得很好,但在后面的版本中却不行?以前版本的 g++ 中是否有 bug 或者 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但我不回答我的问题。如果我需要添加任何特定标签来引起了解此特定问题的人的注意,请告诉我。

最佳答案

带有这样声明的程序

int main = 0;

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

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

摘自C++ 17标准(6.6.1主函数)

  1. … A program that declares a variable main at global scope or that declares the name main with C language linkage (in any namespace) is ill-formed. The name main is not otherwise reserved.

您可以在 C++ 程序中使用此类声明,例如按以下方式

namespace Name
{
int main = 0;
}

关于c++ - int main = 0,已编译但崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59102007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com