【问题标题】:CMake finds boost libraries but Make fails to link themCMake 找到 boost 库,但 Make 无法链接它们
【发布时间】:2016-09-09 06:52:17
【问题描述】:

一个月后回到项目后,我能够成功运行 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 库。我假设这是我的错误的原因。

【问题讨论】:

  • ...(64bit vs 32bit) but am not sure how to identify if this is the issue for me. - CMake 找到的库存储在 BOOST_LIBRARIES 变量中。您可以添加行message(${BOOST_LIBRARIES}) 作为该变量的输出值(在cmake 阶段),并检查库。
  • 此外,您可以在调用find_package(Boost...) 之前尝试set(Boost_DEBUG ON) 以启用FindBoost 的调试输出。
  • 打开调试后,我多次发现此 NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH,感谢您的帮助,我将对此进行调查,看看这是否是我的错误编辑的原因:调查后似乎是好的,因为我没有指定根路径

标签: c++ boost makefile cmake


【解决方案1】:

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

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

【讨论】:

  • 那行得通,一定是在较新的 cmake 版本之一中进行了更改,因为大写 BOOST 曾经可以正常工作,您是否建议我更改我的其他 find_packages 以匹配包名称的拼写,例如作为 OpenSSL_INCLUDE_DIRS?
  • @Jem4687 是的,CMake 正在朝着统一查找生成的变量名称的方向发展,以使用查找模块名称的确切大小写。当然,如果可以并且模块支持它们,最好使用具有使用要求的导入目标(那么您不必关心包含目录等)。根据我链接的文档,Boost 确实支持它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-02
  • 2023-04-03
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
相关资源
最近更新 更多