Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to force my development project to support only C++11 standard



  • Hi All,
    I am using 2019 MSVC compiler for desktop application development. This compiler supports even C++20 feature, as per documentation.

    What should I do, to restrict, so that the current application which I am developing just support C++11 features alone, and not latest standards.

    I tried placing CONFIG += C++11 or QMAKE_CXXFLAGS += -std=c++11 in .pro file.

    But both are still allowing me to use features of C++14, such as make_unique<> or make_shared<>

    Like to know, how we restrict the C++ standards used in our project ?
    I am using Qt 5.15

    Thanks for your time.



  • Hi, no sorry I only tried the g++ command in Terminal, but since Qt Creator compiles using g++ it should work the same.

    However, any setting you try in your .pro file, like CONFIG += c++11 or QMAKE_CXXFLAGS += -std=gnu++11 or other combinations (there are lots of them on StackOverflow :-) will not work, probably because c++17 is a mandatory requirement for Qt 6, so the setting for c++17 (-std=gnu++1z) is more or less "hardwired" into Qt 6.

    Much easier then just to patch the .conf file for Qt Creator, it's in the ~/Qt/6.2.2/gcc_64/mkspecs/common directory on Linux and in the ~/Qt/6.2.2/macos/mkspecs/common directory on the Mac.
    For gcc patch the file g++-base.conf.
    For clang patch the file clang.conf.
    For both files, change the line:

    QMAKE_CXXFLAGS_GNUCXX1Z          = -std=gnu++1z
    

    to

    QMAKE_CXXFLAGS_GNUCXX1Z          = -std=gnu++11
    

    then run qmake again inside Qt Creator, voila!


  • Moderators

    @Vinoth-Rajendran4
    afaik you cannot downgrade/restrict a c++ version used. if the compiler supports it, it is used

    but why should this be an issue afterall? just declare that your project compiles with an old msvc version if you have to.



  • Hi, seems you've chosen the wrong compiler :-(

    With gcc you can downgrade to C++11 standard, by removing the default compiler option -std=gnu++1z and instead use -std=gnu++11

    If you try a simple test program:

    #include <memory>
    int main()
    {
        std::unique_ptr<int> p = std::make_unique<int>(42);
    }
    

    With -std=gnu++1z it compiles fine but with -std=gnu++11 gcc returns the error:
    ../untitled/main.cpp: In function ‘int main()’:
    ../untitled/main.cpp:4:35: error: ‘make_unique’ is not a member of ‘std’
    4 | std::unique_ptr<int> p = std::make_unique<int>(42);
    ../untitled/main.cpp:4:35: note: ‘std::make_unique’ is only available from C++14 onwards

    With Qt on Windows you could try switching to the MinGW compiler, it should behave the same as gcc...



  • @hskoglund Thanks for helping me understand, that it can be done with gcc compiler.

    Not sure why MSVC compiler not supporting it.



  • @raven-worx , Thanks for the suggestion.
    This is just an interview question asked to me. I said it can be done by setting the compiler flag in .pro file.

    But later when I tried it with MSVC, it was not working as per my expectation. Seems GCC supports the option. Thanks to @hskoglund input


  • Lifetime Qt Champion

    Hi,

    From the looks of it, it is supported since VS2017. See the related flag documentation page.



  • @SGaist Yes in MSCV there is a /std:c11 option but that's applicable for C programs only, not for C:++ flavored ones :-(



  • @hskoglund : Have you tried using -std=gnu++11 switch with GCC based compiler in Qt Creator ?

    I tried using CONFIG += c++11 with Android Clang based compiler. Still my program not able to restrict the code to c++11 standard.

    So, it's just not that MSVC alone having issue. Maybe I am missing something here.



  • Hi, no sorry I only tried the g++ command in Terminal, but since Qt Creator compiles using g++ it should work the same.

    However, any setting you try in your .pro file, like CONFIG += c++11 or QMAKE_CXXFLAGS += -std=gnu++11 or other combinations (there are lots of them on StackOverflow :-) will not work, probably because c++17 is a mandatory requirement for Qt 6, so the setting for c++17 (-std=gnu++1z) is more or less "hardwired" into Qt 6.

    Much easier then just to patch the .conf file for Qt Creator, it's in the ~/Qt/6.2.2/gcc_64/mkspecs/common directory on Linux and in the ~/Qt/6.2.2/macos/mkspecs/common directory on the Mac.
    For gcc patch the file g++-base.conf.
    For clang patch the file clang.conf.
    For both files, change the line:

    QMAKE_CXXFLAGS_GNUCXX1Z          = -std=gnu++1z
    

    to

    QMAKE_CXXFLAGS_GNUCXX1Z          = -std=gnu++11
    

    then run qmake again inside Qt Creator, voila!



  • Just noticed you wrote that you're on Qt 5.15 (your first post) that should make it much easier, for Qt5 adding CONFIG += c++11 to your .pro file actually works, I just tried it on Ubuntu 20.04, Qt 5.15.2 and Qt Creator 6.01 on g++ 9.3. Haven't tried clang.

    If you have trouble anyway with Qt 5.15 you can try my .conf file patching above, but for Qt 5.15 I suggest you patch also this line:
    QMAKE_CXXFLAGS_GNUCXX14 = -std=gnu++1y
    to
    QMAKE_CXXFLAGS_GNUCXX14 = -std=gnu++11



  • @hskoglund : Never have I explored mkspecs directory before. Its good learning for me.

    I was not able to restrict my code to c++11 standards, but i was able to restrict my code to follow c++14 standard with MSVC compiler.

    I did setup QMAKE_CXXFLAGS += /std:c++14 in msvc2019\mkspecs\win32-msvc\qmake.conf file. And tried couple of c++17 standard supported code. Compiler error detected.

    Thanks for your time.


Log in to reply