Using C++11 in Qt

Have you also discovered all the new great C++11 stuff? So far I’ve used only the new flavor of auto and some ranged for-loops, but already those I think are excellent improvements.

If you try them in your Qt app however, chances are you will get a compiler error 🙁

This is easy to fix, just add one line to the .pro file in your project. Here’s an example, before the fix:

HEADERS  += mainwindow.h
FORMS    += mainwindow.ui

At the end insert a CONFIG, like this:

HEADERS  += mainwindow.h
FORMS    += mainwindow.ui
CONFIG   += c++11

That should suffice to make your compiler happy. Note: this is applicable for MinGW on Windows, and for Mac and Linux systems. Also, for Macs, make sure it is written "c++11" and not "C++11", otherwise you’ll get this error:

A capital C not ok in OSX

A capital C will get you nowhere in OSX

For Visual Studio 2010 however, that CONFIG statement is a dud, it will not magically make VS2010 understand something it wasn’t built to understand. You’ll need VS2012, VS2013 or VS2015. Also for those later Microsoft compilers C++11 is the default setting, i.e. that CONFIG statement is immaterial. But my advice is to include it anyway, maybe you’ll be switching to the MinGW compiler (which needs it), or perhaps to Linux or Mac later on.

Next up: deployment to other PCs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove you're human * Time limit is exhausted. Please reload CAPTCHA.