I had very simple program as below.
int main = 0;
This program as a.c file compiled fine with gcc-4.8,gcc-5,gcc-6,gcc-7 and gcc-8 and application crashed when executed.
Same program as a.cpp file compiled fine with g++-4.8 and g++-5 and application crashed when executed. But with later version of g++ it gives compile time error.
Below is my questions.
- Why a.cpp compile fine with g++-4.8 and 5, but not with later one? Does it bug in previous version of g++ OR some improvement in c++ standard?
- If it compile successfully then why application get crashed?
Below is details compiler and output of compilation/execution.
+ 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; ^~~~ Note: I already come across this but I doesn't answer my questions. Let me know if I need to add any specific tag to get attention of person with knowledge of this particular problem.
int main = 0;should not compile live: godbolt.org/z/uh3nm6mainfor a variable, you're doing it wrong. It's undefined behaviour. Don't waste your time investigating why it compiles on certain compiles and not on others and why it segfaults