c++ - CMake 找到 boost 库,但 Make 无法链接它们

标签 c++ boost makefile cmake

一个月后回到项目后,我能够成功运行 CMake,输出如下

-- Boost version: 1.61.0
-- Found the following Boost libraries:
--   system
--   thread
--   filesystem
--   chrono
--   date_time
--   atomic
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/LittleNewt/gitness/MangaMeCLI/build

但出于某种原因,在生成的 MakeFile 上运行 Make 时,我得到以下输出

[ 50%] Building CXX object CMakeFiles/mangaMeCLI.dir/src/mangaMeCLI.cpp.o
[100%] Linking CXX executable mangaMeCLI
Undefined symbols for architecture x86_64:
  "boost::filesystem::path::operator/=(char const*)", referenced from:
      _main in mangaMeCLI.cpp.o
  "boost::filesystem::path::operator/=(boost::filesystem::path const&)", referenced from:
      boost::filesystem::path::operator/=(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) in mangaMeCLI.cpp.o
      boost::filesystem::path::operator/=(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in mangaMeCLI.cpp.o
  "boost::filesystem::detail::current_path(boost::system::error_code*)", referenced from:
      boost::filesystem::current_path() in mangaMeCLI.cpp.o
  "boost::filesystem::detail::create_directory(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
      boost::filesystem::create_directory(boost::filesystem::path const&) in mangaMeCLI.cpp.o
  "boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
      boost::filesystem::exists(boost::filesystem::path const&) in mangaMeCLI.cpp.o
      boost::filesystem::is_directory(boost::filesystem::path const&) in mangaMeCLI.cpp.o
  "boost::system::system_category()", referenced from:
      ___cxx_global_var_init.75 in mangaMeCLI.cpp.o
  "boost::system::generic_category()", referenced from:
      ___cxx_global_var_init.73 in mangaMeCLI.cpp.o
      ___cxx_global_var_init.74 in mangaMeCLI.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [mangaMeCLI] Error 1
make[1]: *** [CMakeFiles/mangaMeCLI.dir/all] Error 2
make: *** [all] Error 2

我做了一些研究,发现其他人由于没有为架构(64 位与 32 位)链接正确版本的库而遇到同样的问题,但我不确定如何确定这是否是我的问题。

这是我未更改的 CMakeLists.txt 文件

CMAKE_MINIMUM_REQUIRED(VERSION 3.4.1)
PROJECT(MangaMeCLI)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
INCLUDE_DIRECTORIES(includes)
ADD_EXECUTABLE(mangaMeCLI src/mangaMeCLI.cpp)
MESSAGE("${CMAKE_CXX_FLAGS}")

SET(Boost_USE_MULTITHREADED ON)
FIND_PACKAGE(Boost REQUIRED COMPONENTS system thread filesystem)
INCLUDE_DIRECTORIES(${BOOST_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(mangaMeCLI ${BOOST_LIBRARIES})

SET(OPENSSL_ROOT_DIR /usr/local/Cellar/openssl/*)
FIND_PACKAGE(OpenSSL REQUIRED)
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(mangaMeCLI ${OPENSSL_LIBRARIES})

FIND_PACKAGE(cppnetlib REQUIRED)
INCLUDE_DIRECTORIES(${CPPNETLIB_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(mangaMeCLI ${CPPNETLIB_LIBRARIES})

阅读有用的评论后,我发现 BOOST_LIBRARIES 变量是空的,即使 cmake 打印它找到了我正在寻找的 boost 库。我假设这是我出错的原因。

最佳答案

CMake 变量区分大小写。根据 find module's documentation ,你想要 Boost_LIBRARIES,而不是 BOOST_LIBRARIES:

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(mangaMeCLI ${Boost_LIBRARIES})

关于c++ - CMake 找到 boost 库,但 Make 无法链接它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39405482/

相关文章:

c++ - boost 线程和信号处理

c++ - 在我的 c 程序中静态链接 OpenSSL

makefile - GNU Make 动态规则的问题

c++ - C++中的复制构造函数

c++ - VS2012 C++ 警告 C4005 : '__useHeader' : macro redefinition

c++ - 在结构中使用固定长度的 vector - C++

c++ - 在 C++ 中查找函数定义

c++ - Cucumber-cpp 运行示例所需的软件

c++ - 用C++高效地读取部分文本

c++ - boost 库是否依赖于 std C++ 库?