CMake 未能 link 在 Clion 中提升正则表达式库

CMake failing to link Boost regex library in Clion

我正在努力让 Boost 1.72 版本准备好在我的 C++ 项目中使用。 OS 是 Windows 10. 我使用 Clion 作为 IDE,使用 CMake 3.17 和 gcc 8.1.0 作为工具链。我尝试按照网络上的许多不同说明尝试正确“安装”Boost,最后我完成了这些步骤(所有斜体文件夹名称都是有效的完全限定路径):

  1. 解压 Boost 源代码,进入 boost-sources-dir/tools/build, 运行 bootstrap.bat gcc

  2. 然后 运行 b2 install --prefix="provided-boost-build-folder"

  3. 然后将provided-boost-build-folder/bin添加到PATH变量。

  4. 返回 boost-sources-dir 和 运行 b2 --build-dir="boost-sources -dir\build" --prefix="boost-install-dir" toolset=gcc install --build-type=complete -j 4

  5. 现在我在 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 中的一些示例代码,看看编译和 linking 是否工作:

/* 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;
    }
}

在尝试 运行 这个程序后,它给出了很多 link 错误:

[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 文件中“缺少”符号的定义与错误所在!我做错了什么?

我终于让它工作了。我的 CMakeLists.txt 现在看起来如下:

cmake_minimum_required(VERSION 3.14)
project(DBMSProject)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "--coverage")
set(CMAKE_PREFIX_PATH ${BOOST_LIBRARYDIR}\cmake)

find_package(Boost CONFIG REQUIRED COMPONENTS regex)
include_directories(${Boost_INCLUDE_DIRS})

add_executable(DBMSProject main.cpp /* stuff */)
target_link_libraries(DBMSProject -static)
#target_link_libraries(DBMSProject ${Boost_LIBRARIES}) # haven't tried that but I think it might work as well
target_link_libraries(DBMSProject Boost::regex)

事实证明,如果我不在 CMakeLists.txt 中使用它们,我不需要 CMake 的 -DBOOST... 参数,所以我排除了 -DBOOST_INCLUDEDIR 和 -DBOOST_ROOT 来自他们。