CMake无法链接Clion中的Boost正则表达式库 我试图获得Boost 1.72版本,准备在我的C++项目中使用。操作系统是Windows10。我使用Clion作为IDE,使用CMake 3.17和gcc 8.1.0作为工具链。我尝试在web上按照许多不同的说明正确地“安装”Boost,最后我完成了以下步骤(所有斜体文件夹名称都是有效的完全限定路径):

CMake无法链接Clion中的Boost正则表达式库 我试图获得Boost 1.72版本,准备在我的C++项目中使用。操作系统是Windows10。我使用Clion作为IDE,使用CMake 3.17和gcc 8.1.0作为工具链。我尝试在web上按照许多不同的说明正确地“安装”Boost,最后我完成了以下步骤(所有斜体文件夹名称都是有效的完全限定路径):,c++,boost,cmake,ld,clion,C++,Boost,Cmake,Ld,Clion,解压缩Boost源代码,转到boostsources目录/tools/build,运行bootstrap.bat gcc 然后运行b2安装--prefix=“提供的boost构建文件夹” 然后将提供的boost build folder/bin添加到PATH变量 返回boostsources目录并运行b2--build dir=“boostsources dir\build”--prefix=“boostinstall dir”toolset=gcc安装--build type=compl

解压缩Boost源代码,转到boostsources目录/tools/build,运行bootstrap.bat gcc

  • 然后运行b2安装--prefix=“提供的boost构建文件夹”

  • 然后将提供的boost build folder/bin添加到PATH变量

  • 返回boostsources目录并运行b2--build dir=“boostsources dir\build”--prefix=“boostinstall dir”toolset=gcc安装--build type=complete-j4

  • 现在我在boost安装目录中有了“include”和“lib”文件夹。我打开Clion并在Clion设置中为CMake添加参数:

  • 我的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;
        }
    }
    
    
    /*其他标题*/
    #包括
    . . .
    int main(){
    std::字符串行;
    boost::regex pat(“^Subject:(Re:| Aw:)*(*)”;
    while(std::cin)
    {
    std::getline(std::cin,line);
    小比赛;
    if(boost::regex_匹配(行、匹配、pat))
    
    std::cout我终于让它工作了。我的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中使用-DBOOST…参数,我就不需要CMake的参数,所以我从中排除了-DBOOST_INCLUDEDIR和-DBOOST_ROOT。

    当使用
    find_包(Boost-CONFIG-REQUIRED-COMPONENTS-regex)时会发生什么
    ?对于最新的CMake和Boost版本,使用
    CONFIG
    模式查找Boost是最干净的,使用导入的Boost目标
    Boost::regex
    @squareskittles,这是另一个错误。“找不到”Boost提供的包配置文件。”使用以下任意名称:BoostConfig.cmake boost-config.cmake将安装前缀“boost”添加到cmake_prefix_路径,或将“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)但是if给了我同样的链接错误。是的,你应该在你的CMAKE中设置变量
    CMAKE_PREFIX_PATH
    ,指向
    b2
    构建Boost库的位置。那里应该有一个BoostConfig.CMAKE文件。@squareskittles现在好多了(我开始理解了),我发现我的boost安装目录实际上包含boost的所有CMake配置,但CMake继续抱怨:找到了包配置文件:boost安装目录/lib/CMake/boost_regex-1.72.0/boost_regex-config.CMake,但它将boost_regex_found设置为FALSE,所以包“boost_regex”被认为未找到。package://给出的原因未找到合适的生成变体//
    [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
    
    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)