6

I used Qt 5.7 and gcc 4.9.2. Qt Core module throw Qt requires C++11 support error.

This page say that

gcc 4.9.2 fails to compile Qt.

So I installed gcc 4.8. I check using below command on terminal :

$ g++ --version
g++ (Ubuntu 4.8.4-1ubuntu15) 4.8.4

My kit uses cmake not qmake. I add TARGET_LINK_LIBRARIES ( xxxx yyyy /usr/bin/c++ -std=c++11 to CMakeLists-txt.

I restart my pc and run my application again. Same error is throwed.

/opt/Qt/5.7/gcc_64/include/QtCore/qbasicatomic.h:61: error: #error "Qt requires C++11 support"
 #  error "Qt requires C++11 support"
    ^

How can I solve it?

demonplus
  • 5,613
  • 12
  • 49
  • 68
zakjma
  • 2,030
  • 12
  • 40
  • 81
  • Did you try using C++11 (e.g. compiler flags)? – code_dredd Sep 05 '16 at 17:05
  • @ray I add TARGET_LINK_LIBRARIES ( xxxx yyyy /usr/bin/c++ -std=c++11 to CMakeLists.txt file – zakjma Sep 05 '16 at 17:06
  • Ah, sorry, I must've missed the part during my first glance at the post. – code_dredd Sep 05 '16 at 17:08
  • `TARGET_LINK_LIBRARIES` seems to only be about the linking stage. `-std=c++11` must also be added to the compiler options. You might find an answer there: http://stackoverflow.com/q/10851247/894321 – alexisdm Sep 05 '16 at 17:35
  • 3
    Use `CXX_STANDARD` and `CXX_STANDARD_REQUIRED` CMake variables (https://cmake.org/cmake/help/v3.1/prop_tgt/CXX_STANDARD.html). Manually adding `-std` options will often break your CMake build if different libraries require different standards. – Velkan Sep 05 '16 at 19:03
  • @Velkan SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") is fine. It works. – zakjma Sep 06 '16 at 07:47
  • @zumma, it works on your machine with your config. – Velkan Sep 06 '16 at 08:37

5 Answers5

4

If using QtCreator, you can add this to your .pro file:

CONFIG += c++11

https://wiki.qt.io/How_to_use_C%2B%2B11_in_your_Qt_Projects

NuclearPeon
  • 5,743
  • 4
  • 44
  • 52
3

Its has been a while. How I finally solve it is indicating in CMakeLists.txt the following line just after project(MyProject):

add_compile_options(-std=c++11)

That says to cmake, to create a Makefile that will use c++11 solving issues.

César HM
  • 198
  • 2
  • 16
1

solution for me was (in your .pro file):

QMAKE_CXXFLAGS += -stdlib=libc++
QMAKE_CXXFLAGS += -std=c++11
QMAKE_CXXFLAGS += -mmacosx-version-min=10.7
QMAKE_LFLAGS += -mmacosx-version-min=10.7
Pauland
  • 1,974
  • 16
  • 25
0

Turn c++11 on explicitly:

  1. set(CMAKE_CXX_FLAGS "-std=c++11" CACHE STRING "compile flags" FORCE) after project(...) declaration.
  2. add_library(MyLib SHARED ${PROJECT_HEADERS} ${PROJECT_SOURCES}) ... set_property(TARGET MyLib PROPERTY CXX_STANDARD 11) set_property(TARGET MyLib PROPERTY CXX_STANDARD_REQUIRED ON)
Sergey Belash
  • 1,433
  • 3
  • 16
  • 21
0

SOMETIMES, this will not be a configuration issue as mentioned in other answers. In my case, the problem was one file that happened to have been saved with a .CPP extension rather than .cpp. QMake (Qt5) was misidentifying the file and trying to compile it with the C compiler rather than the C++ compiler. The QMake from Qt4 was not exhibiting this issue. Renaming the file fixed the issue.

My comment at the time was "Could this really be that f&%%& simple!!"