【问题标题】:How to link Boost using CMake?如何使用 CMake 链接 Boost?
【发布时间】:2018-09-25 07:55:12
【问题描述】:

我使用自制软件在 MacOS High Sierra 10.13.3 上安装了 Boost 1.60:brew install boost@1.60

我正在运行 CLion 2018.1.1,我的工具链设置如下:捆绑 CMake 3.10.3、C++ 编译器 /Library/Developer/CommandLineTools/usr/bin/c++、GNU Make 3.81

我的CMakeLists.txt文件如下:

cmake_minimum_required(VERSION 3.10)
project(xxx)
set(CMAKE_CXX_STANDARD 17)
add_executable(${PROJECT_NAME} main.cpp)
Set(BOOST_ROOT /usr/local/Cellar/boost@1.60/1.60.0)
find_package(Boost 1.60.0 REQUIRED COMPONENTS system filesystem regex)
if(Boost_FOUND)
    message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")
    include_directories(${BOOST_ROOT})
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
    target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
endif()

构建似乎成功了: -- Boost version: 1.60.0 -- Found the following Boost libraries: -- system -- filesystem -- regex -- Boost_INCLUDE_DIRS: /usr/local/Cellar/boost@1.60/1.60.0/include -- Boost_LIBRARIES: /usr/local/Cellar/boost@1.60/1.60.0/lib/libboost_system-mt.dylib;/usr/local/Cellar/boost@1.60/1.60.0/lib/libboost_filesystem-mt.dylib;/usr/local/Cellar/boost@1.60/1.60.0/lib/libboost_regex-mt.dylib -- Boost_VERSION: 106000

但我在尝试执行时收到以下链接器错误

架构 x86_64 的未定义符号: “boost::re_detail_106000::perl_matcher, std::__1::allocator >>, boost::regex_traits >>::construct_init(boost::basic_regex >> const&, boost::regex_constants::_match_flags)”,引用自: boost::re_detail_106000::perl_matcher, std::__1::allocator >>, boost::regex_traits >>::perl_matcher(std::__1::__wrap_iter, std::__1::__wrap_iter, boost::match_results, std ::__1::allocator > > >&, boost::basic_regex > > const&, boost::regex_constants::_match_flags, std::__1::__wrap_iter) 在 main.cpp.o “boost::re_detail_106000::perl_matcher, std::__1::allocator > >, boost::regex_traits > >::match()”,引用自: bool boost::regex_match, std::__1::allocator > >, char, boost::regex_traits > >(std::__1::__wrap_iter, std::__1::__wrap_iter, boost::match_results, std::__1 ::allocator > > >&, boost::basic_regex > > const&, boost::regex_constants::_match_flags) 在 main.cpp.o ld:未找到架构 x86_64 的符号

我做错了什么,我该如何解决?

【问题讨论】:

  • 运行make VERBOSE=1 并发布链接行。这将显示您尝试链接的实际库。
  • /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/language_features.dir/link.txt --verbose=1 /Library/Developer/CommandLineTools/usr/ bin/c++ -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/language_features.dir/main.cpp.o CMakeFiles/language_features.dir/test/cpp_sorter_test.cpp.o -o language_features -lgtest -lgtest_main /usr/local /Cellar/boost@1.60/1.60.0/lib/libboost_system-mt.dylib /usr/local/Cellar/boost@1.60/1.60.0/lib/libboost_filesystem-mt.dylib /usr/local/Cellar/boost@1.60 /1.60.0/lib/libboost_regex-mt.dylib

标签: boost cmake clion


【解决方案1】:

我的错:在我的交叉构建中,我强制使用 libc++ 而不是 stdlibc++ if (APPLE) # add_definitions(-D__GLIBCXX__)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2017-10-18
    • 2011-04-23
    • 1970-01-01
    相关资源
    最近更新 更多