[Interest] How best to turn on "-std=gnu++11" when using qmake to build a project?

K. Frank kfrank29.c at gmail.com
Fri Apr 19 00:40:04 CEST 2013


Hi Kai (and Everyone)!

On Wed, Apr 17, 2013 at 2:50 AM, Koehne Kai <Kai.Koehne at digia.com> wrote:
>
>> -----Original Message-----
>> From: interest-bounces+kai.koehne=digia.com at qt-project.org
>>
>> Hello List!
>>
>> When I am using qmake to build a project that uses Qt, what is the best way
>> to turn on the compiler flag "-std=gnu++11"?  (I am using a mingw-w64 build
>> of g++ 4.8.1.)
>
> qmake -r QMAKE_CXXFLAGS+=-std=gnu++11
>
>> Ideally, I would like to do this on a "global" basis, and not have to do it for
>> each project separately (but that's not essential).
>
> I'd personally advise against using any kind of magic here, just pass it to qmake explicitly.

First off, I understand that my schemes lack the Qt good-housekeeping
seal of approval ...  If I hack around with the Qt-provided files, I'd better
be careful and remember what hacks I hacked.

> If you really want to go down this route, you could also create a custom mkspec (say linux-g++-c++11) under mkspecs by copying your default one, and adding QMAKE_CXXFLAGS+=-std=gnu++11 to linux-g++-c++11/qmake.conf. You could then set the QMAKESPEC environment variable to $QTDIR/mkspecs/linux-g++-c++11. Anyhow, the next time you get 'weird' errors you better remember the magic :)

Okay, here's my scheme and how I am hoping it will work.  I don't
really understand how qmake and the configuration process work
together, so please let me know if I've got something wrong.

I will be building natively with mingw-w64, so I believe the only thing
that is relevant is mkspecs\win32-g++-4.6.

1)  Leave mkspecs\win32-g++-4.6\qmake.conf unmodified (i.e., don't
add "-std=gnu++11" to it), run

   configure -platform=win32-g++-4.6 (and things like -no-qt3support)

and

   mingw32-make

This should build Qt normally (i.e., no "-std=gnu++11").

2)  Edit mkspecs\win32-g++-4.6\qmake.conf to contain

   QMAKE_CXXFLAGS *= -std=gnu++11 -fno-keep-inline-dllexport

3)  Use c++11 features in the code for my Qt project, and build with

   qmake
   mingw32-make

My theory is the the qmake-generated makefiles that get processed by
mingw32-make will have "-std=gnu++11" in the g++ compile commands.

More specifically my theory is that running

      configure -platform=win32-g++-4.6

somehow sets up qmake to use whatever is in

   mkspecs\win32-g++-4.6\qmake.conf

at the time qmake is run, even if qmake.conf gets modified after the
cinfigure step (rather than copying the unmodified contents of qmake.conf
somehow into qmake).

So, that's my scheme.  Do you think it will work as I imagine (even if
it doesn't have the Qt good-housekeeping seal of approval)?

> Finally, how about switching to Qt 5 ? Qt 5 is compiled with c++11 enabled by default, and you can enable it for your own projects by doing CONFIG+=c++11 (qmake based projects).

Yes, upgrading to Qt 5 is on my list.  For me, c++11 and Qt 5 are both
big enough steps that I thought I'd be conservative, and take them one
at a time.

> Regards
>
> Kai

Thanks for everyone's advice.

And ...


Happy C++11 Hacking!


.K. Frank



More information about the Interest mailing list