背景:我是一名CS级的学生,试图找出为什么Windows和Linux编译器不一致,因为学习C ++的人可以在他们的作业中输入一些非常奇怪的东西。这不是我写的代码,它的代码是我在弄清楚如何评分(并阻止人们上交!)。
我试图找出为什么the following code snippet在Linux上编译而不在Windows上编译:
$ cat return_type_required.cpp
// on Linux GCC complains
// on Windows neither MSYS nor MinGW complain, even with -Wall
foo() { return true; }
int main() {
foo();
return 0;
}
在Linux(Arch)上我得到了这个:
$ g++-5 --version
g++-5 (GCC) 5.4.0
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 return_type_required.cpp
return_type_required.cpp:4:5: error: ISO C++ forbids declaration of ‘foo’ with no type [-fpermissive]
foo() { return true; }
^
$ g++ --version
g++ (GCC) 6.2.1 20160830
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.
$ g++ return_type_required.cpp
return_type_required.cpp:4:5: error: ISO C++ forbids declaration of ‘foo’ with no type [-fpermissive]
foo() { return true; }
^
但是在MSYS2下的Windows GCC上:
user@windows MSYS [...]
$ g++ return_type_required.cpp
user@windows MSYS [...]
$ g++ return_type_required.cpp -Wall
并在MinGW-w64下
user@windows MINGW64 [...]
$ g++ return_type_required.cpp
user@windows MINGW64 [...]
$ g++ return_type_required.cpp -Wall
并且我在GCC文档或C ++标准中没有成功找到关于该程序应该如何编译的内容。
编辑1,2,3:
仍然无法在Windows上调用编译器错误(我还指定了C ++ 11,但它对C ++ 98和C ++ 1z来说是相同的):
user@windows MSYS [...]
$ g++ return_type_required.cpp -Werror -Wall -Wextra -pedantic -pedantic-errors -std=c++11
user@windows MINGW64 [...]
$ g++ return_type_required.cpp -Werror -Wall -Wextra -pedantic -pedantic-errors -std=c++11
MSYS2的Windows工具链设置如下:
user@windows MSYS [...]
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-msys/5.3.0/lto-wrapper.exe
Target: x86_64-pc-msys
Configured with: /msys_scripts/gcc/src/gcc-5.3.0/configure --build=x86_64-pc-msys --prefix=/usr --libexecdir=/usr/lib --enable-bootstrap --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --with-arch=x86-64 --with-tune=generic --disable-multilib --enable-__cxa_atexit --with-dwarf2 --enable-languages=c,c++,fortran,lto --enable-graphite --enable-threads=posix --enable-libatomic --enable-libcilkrts --enable-libgomp --enable-libitm --enable-libquadmath --enable-libquadmath-support --enable-libssp --disable-win32-registry --disable-symvers --with-gnu-ld --with-gnu-as --disable-isl-version-check --enable-checking=release --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible
Thread model: posix
gcc version 5.3.0 (GCC)
和MINGW64:
user@windows MINGW64 [...]
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-msys/5.3.0/lto-wrapper.exe
Target: x86_64-pc-msys
Configured with: /msys_scripts/gcc/src/gcc-5.3.0/configure --build=x86_64-pc-msys --prefix=/usr --libexecdir=/usr/lib --enable-bootstrap --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --with-arch=x86-64 --with-tune=generic --disable-multilib --enable-__cxa_atexit --with-dwarf2 --enable-languages=c,c++,fortran,lto --enable-graphite --enable-threads=posix --enable-libatomic --enable-libcilkrts --enable-libgomp --enable-libitm --enable-libquadmath --enable-libquadmath-support --enable-libssp --disable-win32-registry --disable-symvers --with-gnu-ld --with-gnu-as --disable-isl-version-check --enable-checking=release --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible
Thread model: posix
gcc version 5.3.0 (GCC)