The Mysteries of MinGW C++ 11

Find Tips and tricks on how to better use the Zeus IDE. Feel free to post your own tips but please do not post bug reports, feature requests or questions here.
Post Reply
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

The Mysteries of MinGW C++ 11

Post by jussij »

This is a follow up to the Setting up MinGW post showing how to get Zeus to work with MinGW.

Lets now consider this C++ 11 code:

Code: Select all

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);

    for (int i : vec )
    {
        cout << i << "\n";
    }
}
Using the compiler settings from the earlier post:

Code: Select all

g++ "$fn" -o "$fdd$fb.exe"
will result in the following error message:

Code: Select all

C:\temp\test.cpp: In function 'int main()':
C:\temp\test.cpp:14:18: error: range-based 'for' loops are not allowed in C++98 mode
     for (int i : vec )
So to get this C++ 11 code to compile we need to add the -std=c++11 option to the command line.

While we're at it lets add a few other options and make the C/C++ Document Type compiler command line read as follows:

Code: Select all

g++ -x c++ -Wall -std=c++11 "$fn" -o "$fdd$fb.exe"
With these compiler options the compile now works and it produces the test.exe file without any errors or warnings:

Code: Select all

 Directory of c:\temp

31/07/2013  11:13 PM            99,711 test.exe
               1 File(s)         99,711 bytes
               0 Dir(s)   1,603,624,960 bytes free
But if we now try and run that executable it runs fine, without error, but produces no output.

Then if we remove the C++ 11 code and use nothing more than a printf it compiles, runs and produces the expected output.

So what could be going wrong :?:

Well, after a long period of Google searching, it appears the MinGW requires a few dll files to run properly.

If it doesn't find those dll files it does not complain, but rather it runs without error but it just doesn't work.

So what is the magic sauce that makes things work :?:

Based on the folder details described in the earlier Setting up MinGW post.

There is a set of runtime dlls found in the following directory:

Code: Select all

C:\Program Files\mingw-builds\x32-4.8.1-posix-dwarf-rev3\mingw32\bin\
If we now use the Zeus Tools, DOS Command Line menu and in the resulting command line dialog we set the arguments to be

Code: Select all

c:\temp\test.exe
and the working directory to be

Code: Select all

C:\Program Files\mingw-builds\x32-4.8.1-posix-dwarf-rev3\mingw32\bin\
we now see the expected output:

Code: Select all

1
2
3
4
What is confusing is that folder is already in the system PATH environment variable so the should have been able to find them :!:

I'm going to have to spend a little more time trying to get my head around how MinGW actually works :(

Cheers Jussi
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

Mystery Solved

I analysed the resulting executable using a Dependency Walker utility.

That tool has an option to display the full path location of the DLL files being used. To see the DLL full path, right click on the dependency panel and select the full path option.

This revealed there were other MinGW DLL files in the current PATH and these older version DLL files where being used ahead of the DLL files that came with the compiler (found in the compiler bin folder).

Another way to locate the errant DLL would be to use the which.exe tool as described here.

To fix the text output issue I just had to make sure the MinGW compiler bin folder appeared before any of the other MinGW DLL folders in the PATH.

For more details about the PATH see here.

Cheers Jussi
Post Reply