在Cake中将C ++ 11程序链接到Qt5会导致错误

时间:2016-05-25 14:20:27

标签: c++ cmake qt5

我写了两个程序,每个程序编译。但是,当我想将它们组合在一起时,我会遇到错误。我认为Qt5会支持Cxx11,但我得到错误。是因为我的发行版在没有Cxx11支持的情况下构建了Qt5吗?

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) 

错误:

/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
       ^

1 个答案:

答案 0 :(得分:1)

Qt5应该支持C ++ 11,除非它已经明确地构建了关闭它的选项。您可能要求使用错误的C ++库。而不是试图明确指定编译器和链接器标志,这意味着你将承担更多的责任让事情正确,让CMake为你做的工作,看看是否能解决你的问题。如果您使用的是合理的CMake版本(3.2或更高版本应该是安全的),请尝试将以下内容放在CMakeLists.txt文件的顶部附近,特别是在project()命令之前:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

这将让CMake选择适当的编译器和链接器标志,这些标志将相互一致。在您的情况下,您只设置了一个编译器标志,因此链接器将选择默认的C ++库,这可能是您想要的,也可能不是。将责任交给CMake至少会将问题排除在外。仍然无法保证CMake会选择与Qt构建时使用的库相同的库,但您的机会非常好。你的问题中的构建输出表明你的问题无论如何都在编译时,所以你的构建还没有进入链接阶段。也就是说,我仍然认为以上内容可能会解决您的问题以及您可能遇到的后续问题。

This article提供了对上述CMake变量和相关CMake功能的更深入讨论(免责声明:我写了这篇文章)。简而言之,他们将默认的C ++版本设置为C ++ 11,然后CMake将选择适合于所使用的特定编译器和链接器的编译器和链接器标志。在CMakeLists.txt文件中创建新目标时,这些变量将用作目标属性的默认值,以确定应为其构建目标的C ++版本。