Can I use clang with -std=c++17 in qmake?

  • I'd like to compile a project using c++17. The project uses qmake from 5.9.2 as its build system and clang 5 as the compiler. Clang 5 supports the -std=c++17 switch to compile in c++17 mode.

    At the moment we can't just add "c++17" to CONFIG in the .pro file (because that's not currently a recognised option for CONFIG), so I've done this:

    CONFIG += c++14 qt dll debug_and_release
    equals(QMAKE_CXX, clang++) {
        message("enabling c++17 support in clang")
        QMAKE_CXXFLAGS += -std=c++17

    Which adds the appropriate -std=c++17 switch to the clang++ command, but unfortunately the compiler command line this generates is:

    clang++ -c -pipe -std=c++17 -g -std=gnu++1y [...]

    which means the -std=gnu++1y overrides the -std=c++17 and the compiler works in c++14 mode.
    I've also tried this slight modification:

    CONFIG += c++14 qt dll debug_and_release
    equals(QMAKE_CXX, clang++) {
        message("enabling c++17 support in clang")
        QMAKE_CXXFLAGS += -std=c++17
        CONFIG -= c++14

    which makes the compiler command:

    clang++ -c -pipe -std=c++17 -g -std=gnu++11 [...]

    so it compiles in c++11 mode, which is worse!

    Is there any way to force qmake to ask the compiler to work in c++17 mode? Is there any way to get options into the compiler command-line after those placed there by what's in CONFIG? Or to stop CONFIG from placing any language-standard option in the compiler command-line? I've searched and read but I can't find anything. Is it just not possible to coerce qmake into issuing compiler command lines that work in c++17 mode?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Please try CONFIG += c++1z

  • That definitely puts the compiler in c++17 (or c++1z) mode, thanks. I didn't know about that option for CONFIG and it doesn't seem to be documented anywhere that google can find. Good to know.

    Sadly it's not solved my overall problem (class template argument type deduction), but now it's probably my code's fault :(

    Anyway, you've answered the question I asked, so many thanks for that.

  • @SGaist I have added CONFIG += c++1z line in the .pro file and on build (after clean and qmake) I am still getting this error:
    error: C1189: #error: class template optional is only available with C++17.
    This is for MSVC. MinGW simply says "no such file or directory" referring to the same class.

    We are using Qt 5.9.2.

    Any ideas? Thank you so much!

  • Lifetime Qt Champion

    What version of MSVC ? Note that MSVC doesn't care about that flag because it always builds code with the feature set of the C++ standard it implements.
    What version of MinGW ?

  • @SGaist I am trying with MinGW 5.3.0 32bit and MSVC 2017 (Compiler 15.0)

  • As explained here, since 15.3, Visual Studio compiler expects flag /std:c++17. I have Visual Studio Build Tools 2017 v15.5.7 installed (although Qt still says it is compiler 15.0).

    Is there any chance to have qmake pass the flag above to cl.exe? In Compile Output, there is no sign of this flag (we have seen it with MinGW though when CONFIG += c++1z was used).

    Thank you!

  • Lifetime Qt Champion

    Thanks for the link !

    After a quick check, it looks like it's not in yet. You can use QMAKE_CXXFLAG and pass the flag manually for the moment.

  • @SGaist It did not work with the flag specification either...
    I have posted the detailed explanation of issues with both compilers in two Stack Overflow posts.

  • Lifetime Qt Champion

    WinRT ? You should have mentioned that earlier. Why don't use one of Qt's WinRT builds ?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.