Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to force my development project to support only C++11 standard
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 3.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by
    #1

    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.

    raven-worxR 1 Reply Last reply
    0
    • hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on last edited by
      #9

      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!

      1 Reply Last reply
      1
      • V Vinoth Rajendran4

        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.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #2

        @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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        V 1 Reply Last reply
        0
        • hskoglundH Online
          hskoglundH Online
          hskoglund
          wrote on last edited by hskoglund
          #3

          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...

          V 1 Reply Last reply
          1
          • hskoglundH hskoglund

            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...

            V Offline
            V Offline
            Vinoth Rajendran4
            wrote on last edited by
            #4

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

            Not sure why MSVC compiler not supporting it.

            1 Reply Last reply
            0
            • raven-worxR raven-worx

              @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.

              V Offline
              V Offline
              Vinoth Rajendran4
              wrote on last edited by
              #5

              @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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                Hi,

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                4
                • hskoglundH Online
                  hskoglundH Online
                  hskoglund
                  wrote on last edited by
                  #7

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

                  V 1 Reply Last reply
                  1
                  • hskoglundH hskoglund

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

                    V Offline
                    V Offline
                    Vinoth Rajendran4
                    wrote on last edited by Vinoth Rajendran4
                    #8

                    @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.

                    1 Reply Last reply
                    0
                    • hskoglundH Online
                      hskoglundH Online
                      hskoglund
                      wrote on last edited by
                      #9

                      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!

                      1 Reply Last reply
                      1
                      • hskoglundH Online
                        hskoglundH Online
                        hskoglund
                        wrote on last edited by
                        #10

                        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

                        V 1 Reply Last reply
                        1
                        • hskoglundH hskoglund

                          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

                          V Offline
                          V Offline
                          Vinoth Rajendran4
                          wrote on last edited by
                          #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.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved