如何在我的Qt项目文件中包含Boost?

时间:2015-09-27 10:39:21

标签: c++ qt boost libraries

我从昨天开始尝试将Boost包含在Qt Creator中。但它不起作用。

INCLUDEPATH += C:\boost_1_59_0

但是我怎么用“.a”文件?

抱歉我的英语不好。

错误讯息:

C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o): duplicate section `.data$_ZZN5boost16cpp_regex_traitsIcE21get_catalog_name_instEvE6s_name[__ZZN5boost16cpp_regex_traitsIcE21get_catalog_name_instEvE6s_name]' has different size
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(regex.o): duplicate section `.rdata$_ZTSN5boost16exception_detail19error_info_injectorISt13runtime_errorEE[__ZTSN5boost16exception_detail19error_info_injectorISt13runtime_errorEE]' has different size
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(regex.o): duplicate section `.rdata$_ZTSN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEEE[__ZTSN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEEE]' has different size
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(regex.o): duplicate section `.rdata$_ZTVN5boost16exception_detail19error_info_injectorISt13runtime_errorEE[__ZTVN5boost16exception_detail19error_info_injectorISt13runtime_errorEE]' has different size

C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(regex.o): duplicate section `.rdata$_ZTVN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEEE[__ZTVN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEEE]' has different size
debug/main.o: In function `ZN5boost9re_detail18basic_regex_parserIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE4failENS_15regex_constants10error_typeEiSsi':
C:/boost_1_59_0/boost/regex/v4/basic_regex_parser.hpp:220: undefined reference to `boost::regex_error::regex_error(std::string const&, boost::regex_constants::error_type, int)'
debug/main.o: In function `ZN5boost9re_detail19basic_regex_creatorIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE16fixup_recursionsEPNS0_14re_syntax_baseE':
C:/boost_1_59_0/boost/regex/v4/basic_regex_creator.hpp:774: undefined reference to `boost::regex_error::regex_error(std::string const&, boost::regex_constants::error_type, int)'
C:/boost_1_59_0/boost/regex/v4/basic_regex_creator.hpp:856: undefined reference to `boost::regex_error::regex_error(std::string const&, boost::regex_constants::error_type, int)'
debug/main.o: In function `ZN5boost9re_detail19basic_regex_creatorIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE16create_startmapsEPNS0_14re_syntax_baseE':
C:/boost_1_59_0/boost/regex/v4/basic_regex_creator.hpp:922: undefined reference to `boost::regex_error::regex_error(std::string const&, boost::regex_constants::error_type, int)'
debug/main.o: In function `ZN5boost9re_detail19basic_regex_creatorIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE15create_startmapEPNS0_14re_syntax_baseEPhPjh':
C:/boost_1_59_0/boost/regex/v4/basic_regex_creator.hpp:1125: undefined reference to `boost::regex_error::regex_error(std::string const&, boost::regex_constants::error_type, int)'
debug/main.o: In function `ZNK5boost9re_detail31cpp_regex_traits_implementationIcE18lookup_collatenameEPKcS4_':
C:/boost_1_59_0/boost/regex/v4/cpp_regex_traits.hpp:679: undefined reference to `boost::re_detail::lookup_default_collate_name(std::string const&)'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x30): undefined reference to `__gxx_personality_sj0'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x6c): undefined reference to `_Unwind_SjLj_Register'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x261): undefined reference to `_Unwind_SjLj_Unregister'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x430): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_create(unsigned int&, unsigned int)'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x4ec): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_create(unsigned int&, unsigned int)'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x5dc): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_create(unsigned int&, unsigned int)'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x74e): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_append(char const*, unsigned int)'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x76d): undefined reference to `std::runtime_error::runtime_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x84c): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_create(unsigned int&, unsigned int)'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x8ae): undefined reference to `_Unwind_SjLj_Resume'
C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o):cpp_regex_traits.cpp:(.text+0x937): undefined reference to `_Unwind_SjLj_Resume'
C:/Qt/Qt5.5.0/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: C:/boost_1_59_0/stage/lib/libboost_regex-mgw51-mt-1_59.a(cpp_regex_traits.o): bad reloc address 0x50 in section `.rdata'
collect2.exe: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:3)

要在Qt项目中链接外部库,您应该提供必需的链接器选项。这些选项可以添加到LIBS文件中的.pro变量。

根据Link Your Program to a Boost Library

  

自动链接

     

大多数Windows编译器和链接器都有所谓的“自动链接”   支持,“这消除了第二个挑战。 Boost中的特殊代码   头文件检测您的编译器选项并使用该信息   将正确库的名称编码到目标文件中;该   链接器从您所在的目录中选择具有该名称的库   告诉它搜索。

     

GCC工具链(Cygwin和MinGW)是值得注意的例外; GCC   用户应参考linking instructions for Unix variant OSes   要使用适当的命令行选项。

对于MinGW,库搜索路径是通过-L path 选项添加的,库名称应该通过-l library 链接,其中 library 是库名称,删除文件名的前导lib和尾随后缀(在这种情况下为.a):

LIBS += -L"C:/boost_library_directory" -lname1 -lname2

根据错误消息的新日志进行更新

您使用两种不同的MinGW编译器:

  • MintW 4.9.2随Qt一起发货
  • 外部MinGW 5.1.0构建Boost

这些编译器之间存在太大的差距,并且您的Boost二进制文件与Qt编译器不兼容。 我能够在我的Qt安装中使用MinGW 5.1.0 for Boost和MinGW 4.8重现您的错误消息。

使用Qt提供的编译器构建Boost。设置适当的工具链路径以构建Boost:

PATH=C:\Qt\Qt5.5.0\Tools\mingw492_32\bin;%PATH%

它应该适用于适当的库版本,例如-lboost_regex-mgw492-mt-1_59

请注意,还有两种类型的Boost二进制文件:release和debug(在文件名中由-d标记)。根据{{​​3}}:

  

虽然有时这些选择不会影响二进制文件,但这是正确的   与其他编译代码的兼容性,你无法依靠它   提升图书馆。

因此,您可能需要用于调试和发布版本的不同项目选项。在Qt .pro中,可以通过doc完成:

# set common library path
LIBS += -L"C:/boost_1_59_0/stage/lib"
CONFIG(debug, debug|release) {
    # debug libraries
    LIBS += -lboost_regex-mgw492-mt-d-1_59
}
CONFIG(release, debug|release) {
    # release libraries
    LIBS += -lboost_regex-mgw492-mt-1_59

    # Release variant of Boost binary libraries is compiled with
    # disabled run-time assertion (NDEBUG is defined).
    # To align binaries with header-only libraries and other headers
    # it is possible to define NDEBUG for project release build.
    # It is not defined in Qt by default.
    DEFINES += NDEBUG
}