关于int main=0在不同GCC/G++版本编译运行问题的技术咨询
main Variable Issue Hey there! Let's unpack what's happening with your code int main = 0; across different compilers and versions.
1. Why does the .cpp file compile in g++ 4.8/5 but fail in newer versions?
This boils down to strictness in enforcing C++ standards.
In the C++ standard, main is a reserved identifier exclusively meant to be the program's entry point function—you're explicitly not allowed to declare it as a global variable.
Older g++ versions (4.8 and 5) had more lenient checks for this rule; they didn't flag the variable declaration of main as an error. This wasn't a "bug" so much as the compiler not strictly enforcing all standard requirements. Starting with g++ 6, the compiler team tightened up these checks to fully comply with the C++ standard, hence the clear compilation error you see:
error: cannot declare ‘::main’ to be a global variable
2. Why does the program crash when it compiles successfully?
Whether you're compiling as C or C++ (with older g++ versions), here's the root cause:
When your program launches, the operating system's runtime linker looks for the main function to execute as the entry point. But in your code, main is a global variable initialized to 0—not a function.
When the runtime tries to "call" main, it's actually attempting to execute code at the memory address stored in the main variable. Since you set it to 0, this means it's trying to run code at memory address 0x0—a protected, invalid address on modern operating systems. This immediately triggers a segmentation fault (accessing restricted memory), causing the program to crash.
Compilation & Execution Output Verification
Here's the full set of compiler versions and their behavior you shared, formatted for clarity:
# GCC (C mode) behavior across versions + 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++ (C++ mode) behavior across versions + 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; ^~~~
内容的提问来源于stack exchange,提问作者Manthan Tilva

