Hi all. This is actually about the tdm mingw compiler rather than code::blocks specifically but since you guys recommend that compiler I hope the question is appropriate here.
So, when I try to compile the following trivial code I expect that it should produce 2 warnings and an error and should fail to compile. In fact this is what happens when I compile it in Linux with gcc 4.8.4.
So why does it compile with no error and no warnings at all, using the same command in windows with tdm64 ver 5.1.0-2? Is this a bug in TDM64 or mingw or do I have something configured wrong?
#include <iostream>
#include <array>
using namespace std;
getSum(array<int,5>& arr)
{
int sumIt(0);
for(auto& elem: arr)
sumIt += elem;
return sumIt;
}
main()
{
array<int,5> myInts{};
for(int i = 0; i < 5; i++) {
myInts[i] = i+1;
}
cout << getSum(myInts) << endl;
return (0);
}
These are the compiler messages I expect (and get in Linux):
$ g++ -g -Wall -Wextra -pedantic -std=c++11 -c main.cpp
main.cpp:6:25: error: ISO C++ forbids declaration of ‘getSum’ with no type [-fpermissive]
getSum(array<int,5>& arr)
^
main.cpp:16:6: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wpedantic]
main()
^
main.cpp: In function ‘int main()’:
main.cpp:18:25: warning: missing initializer for member ‘std::array<int, 5ul>::_M_elems’ [-Wmissing-field-initializers]
array<int,5> myInts{};
^
^
Sorry, I don't think this will help, but ...
-------------- Build: Debug in cordova (compiler: GNU GCC Compiler)---------------
g++.exe -fexceptions -pedantic -Wextra -Wall -std=c++11 -c E:\progs\c++\cordova\main.cpp -o obj\Debug\main.o
g++.exe -o bin\Debug\cordova.exe obj\Debug\main.o
Output file is bin\Debug\cordova.exe with size 2.58 MB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
As I said, same command as in Linux.
It's not a Code::Blocks issue. It compiles without errors or warnings on the command line also.
The problem is the opposite: the MinGW compilers are LESS compliant with C++ standards than the older GCC compiler.
Under every C++ standard going back to 98, the compiler should at least warn that "ISO C++ forbids declaration of [function] with no type". GCC 4.8.4 warns on declaration of main with no type but defaults to int and compiles. But it errors on declaration of any other function with no type and refuses to compile. And the "missing field initializers" warning should be given under -std=c++11 or later.
All 3 of these MinGW compilers compile the code with no errors, no warnings.
Here's an even simpler version of the program. It doesn't use std::array but should still produce the same error and warning messages. But the MinGW compilers build it with no errrors or warnings at all.
#include <iostream>
struct array5 {
int nums[5];
int& operator[](int idx) {
return nums[idx];
}
int const& operator[](int idx) const{
return nums[idx];
}
};
getSum(array5 arr) // no return type ERROR
{
int sumIt(0);
int sz = 5;
for(int i = 0; i < sz; i++)
sumIt += arr[i];
return sumIt;
}
main() // no return type WARNING
{
array5 myInts{}; // missing field initializers WARNING
for(int i = 0; i < 5; i++) {
myInts[i] = i+90;
}
std::cout << getSum(myInts) << std::endl;
return (0);
}
Edited to add this code.
The problem exists in MSys2 MinGW 64.
The option "-Wreturn-type" does NOT even get the warning to appear.
x86_64-w64-mingw32-g++.exe (Rev1, Built by MSYS2 project) 5.3.0
I would guess the problem is in MinGW64 code.
I finally got TDM Compiler installed output below:
-------------- Clean: Debug in warntest (compiler: GNU GCC Compiler MinGW64 TDM 5.1)---------------
Cleaned "warntest - Debug"
Running target pre-build steps
g++.exe --version
g++.exe (tdm64-1) 5.1.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.
-------------- Build: Debug in warntest (compiler: GNU GCC Compiler MinGW64 TDM 5.1)---------------
g++.exe -Wall -fexceptions -pedantic -Wextra -Wall -std=c++11 -g -Wreturn-type -fno-permissive -c C:\SourceCode\test\warntest\main.cpp -o obj\Debug\main.o
C:\SourceCode\test\warntest\main.cpp:15:2: warning: #warning is a GCC extension
#warning "this is a warning"
^
C:\SourceCode\test\warntest\main.cpp:15:2: warning: #warning "this is a warning" [-Wcpp]
g++.exe -o bin\Debug\warntest.exe obj\Debug\main.o
Output file is bin\Debug\warntest.exe with size 2.61 MB
Process terminated with status 0 (0 minute(s), 2 second(s))
0 error(s), 2 warning(s) (0 minute(s), 2 second(s))
Tim S.