0
votes

In particular, how can g++ be made to reject this invalid-as-standard-C++ code:

main(){}

?

I'm using MinGW g++ 6.2.0, but I imagine an answer will apply to all currently used variants of g++.


Example of what I've tried:

[C:\my\temp]
> g++ --version | find "++"
g++ (i686-win32-dwarf-rev1, Built by MinGW-W64 project) 6.2.0

[C:\my\temp]
> type foo.cpp
main() {}

[C:\my\temp]
> dir /b
foo.cpp

[C:\my\temp]
> g++ foo.cpp -Wall -Wextra -Wreturn-type -pedantic

[C:\my\temp]
> dir /b
a.exe
foo.cpp

[C:\my\temp]
> _

Version info with verbose compiler output:

C:\my\temp]
> g++ -v --version
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/Program\ Files\ (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/lto-wrapper.exe
g++ (i686-win32-dwarf-rev1, Built by MinGW-W64 project) 6.2.0
Copyright (C) 2016 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.


Target: i686-w64-mingw32
Configured with: ../../../src/gcc-6.2.0/configure --host=i686-w64-mingw32 --build=i686-w64-mingw32 --target=i686-w64-mingw32 --prefix=/mingw32 --with-sysroot=/c/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=win32 --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --enable-libstdcxx-filesystem-ts=yes --disable-sjlj-exceptions --with-dwarf2 --disable-isl-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=i686 --with-tune=generic --with-libiconv --with-system-zlib --with-gmp=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-mpfr=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-mpc=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-isl=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-pkgversion='i686-win32-dwarf-rev1, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/c/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32/opt/include -I/c/mingw620/prerequisites/i686-zlib-static/include -I/c/mingw620/prerequisites/i686-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/c/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32/opt/include -I/c/mingw620/prerequisites/i686-zlib-static/include -I/c/mingw620/prerequisites/i686-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/c/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32/opt/lib -L/c/mingw620/prerequisites/i686-zlib-static/lib -L/c/mingw620/prerequisites/i686-w64-mingw32-static/lib -Wl,--large-address-aware'
Thread model: win32
gcc version 6.2.0 (i686-win32-dwarf-rev1, Built by MinGW-W64 project)
COLLECT_GCC_OPTIONS='-v' '--version' '-shared-libgcc' '-mtune=generic' '-march=i686'
 C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/cc1.exe -quiet -v -iprefix C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/ -U_REENTRANT help-dummy -quiet -dumpbase help-dummy -mtune=generic -march=i686 -auxbase help-dummy -version --version -o C:\Users\AlfP\AppData\Local\Temp\cc4K0l79.s
GNU C11 (i686-win32-dwarf-rev1, Built by MinGW-W64 project) version 6.2.0 (i686-w64-mingw32)
        compiled by GNU C version 6.2.0, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version 0.15
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-v' '--version' '-shared-libgcc' '-mtune=generic' '-march=i686'
 C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/bin/as.exe -v --version -o C:\Users\AlfP\AppData\Local\Temp\ccn2QnTg.o C:\Users\AlfP\AppData\Local\Temp\cc4K0l79.s
GNU assembler version 2.25 (i686-w64-mingw32) using BFD version (GNU Binutils) 2.25
GNU assembler (GNU Binutils) 2.25
Copyright (C) 2014 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `i686-w64-mingw32'.
COMPILER_PATH=C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/bin/
LIBRARY_PATH=C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/../lib/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../lib/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../
COLLECT_GCC_OPTIONS='-v' '--version' '-shared-libgcc' '-mtune=generic' '-march=i686'
 C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/collect2.exe -plugin C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/liblto_plugin-0.dll -plugin-opt=C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\AlfP\AppData\Local\Temp\ccXVaxln.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-liconv -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt --sysroot=C:/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32 -m i386pe -Bdynamic -u ___register_frame_info -u ___deregister_frame_info --version C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/../lib/crt2.o C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/crtbegin.o -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0 -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/../lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../.. C:\Users\AlfP\AppData\Local\Temp\ccn2QnTg.o -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -liconv -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/crtend.o
collect2 version 6.2.0
C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/bin/ld.exe -plugin C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/liblto_plugin-0.dll -plugin-opt=C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\AlfP\AppData\Local\Temp\ccXVaxln.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-liconv -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt --sysroot=C:/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32 -m i386pe -Bdynamic -u ___register_frame_info -u ___deregister_frame_info --version C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/../lib/crt2.o C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/crtbegin.o -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0 -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/../lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../.. C:\Users\AlfP\AppData\Local\Temp\ccn2QnTg.o -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -liconv -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/crtend.o
GNU ld (GNU Binutils) 2.25
Copyright (C) 2014 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
COLLECT_GCC_OPTIONS='-v' '--version' '-shared-libgcc' '-mtune=generic' '-march=i686'

[C:\my\temp]
> _

2
@Kos: No. Please double-check what you believe before you report it as alleged fact.Cheers and hth. - Alf
GCC v6.2.0 on this website generates an error (or a warning if you disable pedantic). Don't know which architecture it runs or whether it's MinGW, but I don't see how that would affect something like this.Biffen
We've already established what applies to commonly used variants of g++, now we're just debugging why it apparently doesn't work with your MinGW. Can you please edit the question and make that clear? I feel it's misleading right now, since normally it's a pretty basic setting.Kos
You should change the title to request answers clearly for the g++ of Mingw. And not -1 everyone who gives an answer about gnu g++. Because --pedantic-errors crearly works in gnu's versionuser183833
MinGW is a port of GCC that's maintained separately. I feel the questions we should be asking is "why does this particular port behave differently than the upstream in this simple case?".Kos

2 Answers

8
votes

You can pass -pedantic-errors, which gives the following error message on g++ 6.3:

main.cpp:1:6: error: ISO C++ forbids declaration of 'main' with no type [-Wpedantic]
 main(){}

Live demo

Here is the documentation for -pedantic-errors:

Give an error whenever the base standard (see -Wpedantic) requires a diagnostic, in some cases where there is undefined behavior at compile-time and in some other cases that do not prevent compilation of programs that are valid according to the standard. This is not equivalent to -Werror=pedantic, since there are errors enabled by this option and not enabled by the latter and vice versa.

2
votes

Once you enable warnings (-Wall), g++ will complain about this code. Additionally you can have the warnings be treated as errors (-Werror), so that the compilation fails (a good thing to have).

Example with g++ 5.4.0:

kos@Starbuck ~$ cat bar.cpp                                                    
main(){}
kos@Starbuck ~$ g++ bar.cpp                                                    
kos@Starbuck ~$ g++ bar.cpp -Wall -Werror                                      
bar.cpp:1:6: error: ISO C++ forbids declaration of ‘main’ with no type [-Werror=return-type]
 main(){}
      ^
cc1plus: all warnings being treated as errors