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

Gre*_*vak 4 c++ qt boost libraries

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

INCLUDEPATH += C:\boost_1_59_0
Run Code Online (Sandbox Code Playgroud)

但是如何处理“.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
Run Code Online (Sandbox Code Playgroud)

Ore*_*era 5

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

\n\n

根据将您的程序链接到 Boost 库

\n\n
\n

自动链接

\n\n

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

\n\n

GCC 工具链(Cygwin 和 MinGW)是明显的例外;GCC\n 用户应参阅Unix 变体操作系统的链接说明, \n 以了解要使用的适当命令行选项。

\n
\n\n

对于 MinGW,库搜索路径由 -L路径选项添加,库名称应由 -l library链接,其中是删除文件名的前导lib和尾随后缀的库名称(.a在本例中):

\n\n
LIBS += -L"C:/boost_library_directory" -lname1 -lname2\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

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

\n\n

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

\n\n
    \n
  • MinGW 4.9.2 随 Qt 一起提供
  • \n
  • 外部MinGW 5.1.0构建Boost
  • \n
\n\n

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

\n\n

使用 Qt 提供的编译器构建 Boost。设置正确的工具链路径来构建 Boost:

\n\n
PATH=C:\\Qt\\Qt5.5.0\\Tools\\mingw492_32\\bin;%PATH%\n
Run Code Online (Sandbox Code Playgroud)\n\n

它应该与正确的库版本一起使用,例如-lboost_regex-mgw492-mt-1_59.

\n\n

请注意,Boost 二进制文件也有两种类型:发布和调试(-d在文件名中标记)。根据文档

\n\n
\n

虽然有时这些选择确实不会影响二进制文件与其他编译代码的兼容性,但您不能指望 Boost 库能够做到这一点。

\n
\n\n

因此,您可能需要不同的项目选项来进行调试和发布构建。在 Qt 中.pro,可以通过测试 CONFIG 变量来完成:

\n\n
# set common library path\nLIBS += -L"C:/boost_1_59_0/stage/lib"\nCONFIG(debug, debug|release) {\n    # debug libraries\n    LIBS += -lboost_regex-mgw492-mt-d-1_59\n}\nCONFIG(release, debug|release) {\n    # release libraries\n    LIBS += -lboost_regex-mgw492-mt-1_59\n\n    # Release variant of Boost binary libraries is compiled with\n    # disabled run-time assertion (NDEBUG is defined).\n    # To align binaries with header-only libraries and other headers\n    # it is possible to define NDEBUG for project release build.\n    # It is not defined in Qt by default.\n    DEFINES += NDEBUG\n}\n
Run Code Online (Sandbox Code Playgroud)\n