dgrat
dgrat

Reputation: 2254

Linking C++11 program to Qt5 in CMake leads to error

I wrote two programs and eachs compiles. However when I want to bring them together I get errors. I thought Qt5 would support Cxx11, but I get errors. Is it because my distribution was building Qt5 without Cxx11 support?!

CMAKE_MINIMUM_REQUIRED (VERSION 3.0)
PROJECT (SOMNetCPU)

FIND_PACKAGE(BZip2 REQUIRED)
FIND_PACKAGE(Qt5Widgets REQUIRED)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

SET( ANGUISourceFiles
  QSOMReader.cpp
)

add_library (SOMReader ${ANGUISourceFiles})
TARGET_LINK_LIBRARIES (SOMReader Qt5::Widgets)

ADD_EXECUTABLE (SOMNetCPU SOMNetCPU.cpp)
TARGET_LINK_LIBRARIES (SOMNetCPU ANNet SOMReader) 

Errors:

/usr/include/c++/5/bits/stl_tree.h:1018:7: error: expected identifier before string constant
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_tree.h:1018:7: error: expected ‘,’ or ‘...’ before string constant
/usr/include/c++/5/bits/stl_tree.h:1018:7: error: expected ‘;’ at end of member declaration
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_tree.h:1029:7: error: expected identifier before string constant
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_tree.h:1029:7: error: expected ‘,’ or ‘...’ before string constant
/usr/include/c++/5/bits/stl_tree.h:1029:7: error: expected ‘;’ at end of member declaration
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_tree.h:1029:7: error: ‘int std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::__abi_tag__(int)’ cannot be overloaded
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_tree.h:1018:7: error: with ‘int std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::__abi_tag__(int)’
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_tree.h:1053:7: error: expected identifier before string constant
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_tree.h:1053:7: error: expected ‘,’ or ‘...’ before string constant
/usr/include/c++/5/bits/stl_tree.h:1053:7: error: expected ‘;’ at end of member declaration
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_tree.h:1053:7: error: ‘int std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::__abi_tag__(int)’ cannot be overloaded
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_tree.h:1018:7: error: with ‘int std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::__abi_tag__(int)’
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_map.h:712:7: error: expected identifier before string constant
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_map.h:712:7: error: expected ‘,’ or ‘...’ before string constant
/usr/include/c++/5/bits/stl_map.h:712:7: error: expected ‘;’ at end of member declaration
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_multimap.h:618:7: error: expected identifier before string constant
       _GLIBCXX_ABI_TAG_CXX11
       ^
/usr/include/c++/5/bits/stl_multimap.h:618:7: error: expected ‘,’ or ‘...’ before string constant
/usr/include/c++/5/bits/stl_multimap.h:618:7: error: expected ‘;’ at end of member declaration
       _GLIBCXX_ABI_TAG_CXX11
       ^

Upvotes: 0

Views: 597

Answers (1)

Craig Scott
Craig Scott

Reputation: 10177

Qt5 should support C++11 unless it has been explicitly built with options to turn that off. It's likely that you are asking for the wrong C++ library. Rather than trying to explicitly specify the compiler and linker flags, which means you would take on more responsibility for getting things right, let CMake do the work for you and see if that fixes your problem. If you are using a reasonably recent CMake version (3.2 or later should be safe), try putting the following near the top of your CMakeLists.txt file, in particular before the project() command:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

This will let CMake pick appropriate compiler and linker flags which will be consistent with each other. In your case, you've only set a compiler flag, so the linker is going to pick up the default C++ library and this might or might not be what you want. Handing the responsibility over to CMake will at least take that problem out of the picture. There is still no guarantee that CMake will pick the same library as was used when Qt was built, but your chances are pretty good. The build output in your question suggests your problem is at compile time anyway, so your build hasn't even gotten to the link stage yet. That said, I still think the above is likely to fix your problem and later problems you would probably have faced.

This article provides a deeper discussion of the above CMake variables and related CMake functionality (disclaimer: I wrote the article). In short, they are setting the default C++ version to C++11 and CMake will then choose compiler and linker flags appropriate to the particular compiler and linker being used. When a new target is created in your CMakeLists.txt file, these variables are used as the defaults for the target properties which determine which C++ version the target should be built for.

Upvotes: 1

Related Questions