简体   繁体   中英

CONFIG += c++11 (still) doesn't work on Linux

I've read several discussion on the topic, some saying that qmake < 3.0 doesn't support the directive correctly. I've just reinstalled Qt 5.9.1 for g++-64, but the problem is still there. Also, I've tried playing with various mkspecs/xxx/xxx.conf files with no luck.

Adding CONFIG += c++11 to a .pro doesn't really add the CXX flag -std=c++11 in the Makefile, so one get the error

error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

when compiling.

I'm working on Linux x64 (an Ubuntu distro), with the last version of Qt libraries (5.9.1). Same project compiles with no errors on Windows with MSVC2015 and MinGw.

It works if I declare QMAKE_CXXFLAGS += -std=c++11 instead of CONFIG += c++11 , but it is suggested to use the former declaration, since qmake will automatically deal with CXX flags via CONFIG directive. Awfully, looks like it's not doing so...

CONFIG += c++11 should be enough so that your compiler is called with the proper flags to enable C++11 support.

However, since 5.7, Qt requires C++11 support so even without CONFIG += c++11 you should get your compiler called with C++11 support.

What I think is that for your compiler, qmake "thinks" c++11 support is the default behavior and qmake doesn't add the -std=c++11 flag.

If you are curious you can check the mkspecs files. As @Swift mentioned you have to check yours as they might have been corrected. In the ones i have in my Qt install, it seems that all is ok as in mkspecs/common/g++-base.conf I have QMAKE_CXXFLAGS_CXX11 = -std=c++11 and QMAKE_CXXFLAGS_GNUCXX11 = -std=gnu++11 .

If I am right that means that you could bypass qmake behavior by trying to use CONFIG += c++14 (or CONFIG += c++1z ). You could also try CONFIG += strict_c++ (or CONFIG -= strict_c++ ), this option will disable (or enable) GNU extensions and thus force qmake to add -std=c++11 (so that g++ will not use the implicit -std=gnu++11 ).

CONFIG variable is meant to configure Qt code ( headers, may be metacode), so c++11 in it should be added to support c++11. Compiler flag is a separate item, and may be required or may not be requred depending on compiler and its version, newer gcc defaults to gnu version of standard ( with extensions), eg gcc 6.0 - to gnu++14.

I figured it out. It was my fault (obviously...)

As said, I've installed last version of Qt (5.9.1) via unified installer script. Still, my QtCreator was using as default an old kit with Qt4 (!!), installed via package manager who-remembers-when, so it was actually using an old version of qmake that doesn't deal with CONFIG directive. Forcing qtchooser to use qt5 doesn't modify the tools set in the used kit (learned something!). Using the last kit everything works as expected. Using qmake via command line, after setting qtchooser to use qt5, works as expected, too; I just wasn't looking for the correct Makefile (....).

So, I can confirm that the problem was the qmake version, as said in others threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM