【发布时间】:2020-06-17 07:28:49
【问题描述】:
我正在尝试准备好在我的 C++ 项目中使用的 Boost 1.72 版本。操作系统是 Windows 10。我使用 Clion 作为 IDE,使用 CMake 3.17 和 gcc 8.1.0 作为工具链。我尝试按照网络上的许多不同说明尝试正确“安装”Boost,最后我完成了这些步骤(所有斜体文件夹名称都是有效的完整路径):
-
解压Boost源代码,进入boost-sources-dir/tools/build,运行bootstrap.bat gcc
-
然后运行 b2 install --prefix="provided-boost-build-folder"
-
然后将 provided-boost-build-folder/bin 添加到 PATH 变量中。
-
返回 boost-sources-dir 并运行 b2 --build-dir="boost-sources-dir\build" --prefix="boost-install-dir" 工具集=gcc install --build-type=complete -j 4
-
现在我在 boost-install-dir 中有“include”和“lib”文件夹。我打开 Clion 并在 Clion 设置中为 CMake 添加参数:
-DBOOST_INCLUDEDIR="boost-install-dir\include"
-DBOOST_LIBRARYDIR="boost-install-dir\lib"
-DBOOST_ROOT="boost-install-dir"
我的 CMakeLists.txt 如下:
cmake_minimum_required(VERSION 3.14)
project(DBMSProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "--coverage")
find_package(Boost)
# I also tried to use the line below instead of a line above, but it gave me a strange error
# "Could NOT find Boost (missing: regex) (found version "1.72.0")"
# find_package(Boost REQUIRED COMPONENTS regex)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(DBMSProject main.cpp /* some other stuff */)
target_link_libraries(DBMSProject -static)
#target_link_libraries(DBMSProject ${Boost_LIBRARIES}) # that didn't work
target_link_libraries(DBMSProject Boost::boost ${Boost_REGEX_LIBRARY}) # neither that does
只需在 main.cpp 中添加一些示例代码,看看编译和链接是否有效:
/* Other headers */
#include <boost/regex.hpp>
. . .
int main() {
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
在尝试运行这个程序后,它给出了很多链接错误:
[100%] Linking CXX executable DBMSProject.exe
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::cpp_regex_traits_char_layer<char>::cpp_regex_traits_char_layer(boost::re_detail_107200::cpp_regex_traits_base<char> const&)':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/cpp_regex_traits.hpp:370: undefined reference to `boost::re_detail_107200::cpp_regex_traits_char_layer<char>::init()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::raw_storage::extend(unsigned long long)':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/regex_raw_buffer.hpp:131: undefined reference to `boost::re_detail_107200::raw_storage::resize(unsigned long long)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::save_state_init::save_state_init(boost::re_detail_107200::saved_state**, boost::re_detail_107200::saved_state**)':
C:/Users//Douments/Programs/boost/include/boost-1_72/boost/regex/v4/perl_matcher_non_recursive.hpp:110: undefined reference to `boost::re_detail_107200::get_mem_block()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::save_state_init::~save_state_init()':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/perl_matcher_non_recursive.hpp:118: undefined reference to `boost::re_detail_107200::put_mem_block(void*)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_imp()':
C:/Users//ocuments/Programs/boost/include/boost-1_72/boost/regex/v4/perl_matcher_common.hpp:221: undefined reference to `boost::re_detail_107200::verify_options(unsigned int, boost::regex_constants::_match_flags)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::unwind_extra_block(bool)':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/perl_matcher_non_recursive.hpp:1371: undefined reference to `boost::re_detail_107200::put_mem_block(void*)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `void boost::re_detail_107200::raise_error<boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > >(boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::error_type)':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/pattern_except.hpp:75: undefined reference to `boost::re_detail_107200::raise_runtime_error(std::runtime_error const&)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::extend_stack()':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/perl_matcher_non_recursive.hpp:236: undefined reference to `boost::re_detail_107200::get_mem_block()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::basic_regex_parser<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::fail(boost::regex_constants::error_type, long long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, long long)':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_parser.hpp:241: undefined reference to `boost::regex_error::regex_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::regex_constants::error_type, long long)'
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_parser.hpp:242: undefined reference to `boost::regex_error::raise() const'
C:/Users//Douments/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_parser.hpp:241: undefined reference to `boost::regex_error::~regex_error()'
C:/Users//v4/basic_regex_parser.hpp:241: undefined reference to `boost::regex_error::~regex_error()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_dtail_107200::basic_regex_creator<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::fixup_recursions(boost::re_detail_107200::re_syntax_base*)':
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:785: undefined reference to `boost::regex_error::regex_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::regex_constants::error_type, long long)'
C:/Users//v4/basic_regex_creator.hpp:785: undefined reference to `boost::regex_error::~regex_error()'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:874: undefined reference to `boost::regex_error::regex_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::regex_constants::error_type, long long)'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:875: undefined reference to `boost::/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:874: undefined reference to `boost::regex_error::~regex_error()'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:785: undefined reference to `boost::regex_error::~regex_error()'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:874: undefined reference to `boost::regex_error::~regex_error()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::basic_regex_creator<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::create_startmaps(boost::re_detail_107200::re_syntax_base*)':
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:940: undefined reference to `boost::regex_error::regex_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::regex_constants::error_type, long long)'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:941: undefined reference to `boost::regex_error::raise() const'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:940: undefined reference to `boost::re
但我可以用我的眼睛清楚地看到与错误所在相同的 .hpp 文件中“缺失”符号的定义!我做错了什么?
【问题讨论】:
-
当您使用
find_package(Boost CONFIG REQUIRED COMPONENTS regex)时会发生什么?使用最新的 CMake 和 Boost 版本,使用CONFIG模式查找 Boost 并使用导入的 Boost 目标Boost::regex是最干净的。 -
@squareskittles,另一个错误。 “找不到由“Boost”提供的具有以下任何名称的包配置文件: BoostConfig.cmake boost-config.cmake 将“Boost”的安装前缀添加到 CMAKE_PREFIX_PATH 或将“Boost_DIR”设置为包含以下任一名称的目录以上文件。如果“Boost”提供单独的开发包或SDK,请确保已安装。”
-
也尝试使用 target_link_libraries() 命令如下: target_link_libraries(DBMSProject -static -Lboost-install-dir\\lib\\libboost_regex-mgw81-mt-d-x64-1_72.dll) 但是如果给了我同样的链接错误。
-
是的,您应该在 CMake 中设置变量
CMAKE_PREFIX_PATH,以指向b2构建 Boost 库的位置。那里应该有一个 BoostConfig.cmake 文件。 -
@squareskittles 现在好多了(我开始明白了),我发现我的 boost-install-dir 实际上包含 Boost 的所有 CMake 配置,但 CMake 继续抱怨:找到包配置文件:boost-install-dir/lib/cmake/boost_regex-1.72.0/boost_regex-config.cmake 但它将 boost_regex_FOUND 设置为 FALSE,因此认为“boost_regex”包未找到。包给出的原因://没有找到合适的构建变体。 //