CMake无法在Clion中链接Boost regex库

问题描述 投票:0回答:1

我正在尝试准备在我的C ++项目中使用Boost 1.72版本。操作系统是Windows10。我将Clion用作IDE,将CMake 3.17和gcc 8.1.0用作工具链。我尝试遵循网上的许多不同说明,尝试正确地“安装” Boost,并最终完成了这些步骤(所有斜体文件夹名称均为有效的全限定路径):

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

2)然后运行b2 install --prefix =“ proveded-boost-build-folder

3)然后将proved-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中仅提供了一些示例代码,以查看编译和链接是否有效:

/* 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/dsomov/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/dsomov/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/dsomov/Documents/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/dsomov/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/dsomov/Documents/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/dsomov/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/dsomov/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/dsomov/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/dsomov/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/dsomov/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/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_parser.hpp:241: undefined reference to `boost::regex_error::~regex_error()'
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/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_detail_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/dsomov/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/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:786: undefined reference to `boost::regex_error::raise() const'
C:/Users/dsomov/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/dsomov/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/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:875: undefined reference to `boost::regex_error::raise() const'
C:/Users/dsomov/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/dsomov/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/dsomov/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/dsomov/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/dsomov/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/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:940: undefined reference to `boost::regex_error::~regex_error()'
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:940: 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_startmap(boost::re_detail_107200::re_syntax_base*, unsigned char*, unsigned int*, unsigned char)':
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:1141: 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/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:1142: undefined reference to `boost::regex_error::raise() const'
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:1141: undefined reference to `boost::regex_error::~regex_error()'
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:1141: undefined reference to `boost::regex_error::~regex_error()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::cpp_regex_traits_implementation<char>::error_string[abi:cxx11](boost::regex_constants::error_type) const':
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/cpp_regex_traits.hpp:447: undefined reference to `boost::re_detail_107200::get_default_error_string(boost::regex_constants::error_type)'
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/cpp_regex_traits.hpp:449: undefined reference to `boost::re_detail_107200::get_default_error_string(boost::regex_constants::error_type)'
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> > >::insert_state(long long, boost::re_detail_107200::syntax_element_type, unsigned long long)':
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:322: undefined reference to `boost::re_detail_107200::raw_storage::insert(unsigned long long, unsigned long long)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::object_cache<boost::re_detail_107200::cpp_regex_traits_base<char>, boost::re_detail_107200::cpp_regex_traits_implementation<char> >::get(boost::re_detail_107200::cpp_regex_traits_base<char> const&, unsigned long long)':
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/pending/object_cache.hpp:66: undefined reference to `boost::scoped_static_mutex_lock::scoped_static_mutex_lock(boost::static_mutex&, bool)'
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/pending/object_cache.hpp:66: undefined reference to `boost::scoped_static_mutex_lock::~scoped_static_mutex_lock()'
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/pending/object_cache.hpp:66: undefined reference to `boost::scoped_static_mutex_lock::~scoped_static_mutex_lock()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::cpp_regex_traits_implementation<char>::lookup_collatename[abi:cxx11](char const*, char const*) const':
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/cpp_regex_traits.hpp:680: undefined reference to `boost::re_detail_107200::lookup_default_collate_name(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::cpp_regex_traits_implementation<char>::init()':
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/cpp_regex_traits.hpp:721: undefined reference to `boost::re_detail_107200::raise_runtime_error(std::runtime_error const&)'
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/cpp_regex_traits.hpp:736: undefined reference to `boost::re_detail_107200::get_default_error_string(boost::regex_constants::error_type)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::cpp_regex_traits<char>::get_catalog_name[abi:cxx11]()':
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/cpp_regex_traits.hpp:1117: undefined reference to `boost::scoped_static_mutex_lock::scoped_static_mutex_lock(boost::static_mutex&, bool)'
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/cpp_regex_traits.hpp:1117: undefined reference to `boost::scoped_static_mutex_lock::~scoped_static_mutex_lock()'
C:/Users/dsomov/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/cpp_regex_traits.hpp:1117: undefined reference to `boost::scoped_static_mutex_lock::~scoped_static_mutex_lock()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\DBMSProject.dir\build.make:119: DBMSProject.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:92: CMakeFiles/DBMSProject.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:99: CMakeFiles/DBMSProject.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:134: DBMSProject] Error 2

但是我可以用自己的眼睛清楚地看到错误所在的.hpp文件中“不存在”符号的定义!我在做什么错?

c++ boost cmake ld clion
1个回答
0
投票

我终于让它工作了。我的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。

© www.soinside.com 2019 - 2024. All rights reserved.