CMake找到了boost库,但Make无法链接它们

时间:2016-09-09 06:52:17

标签: 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库。我假设这是我错误的原因。

1 个答案:

答案 0 :(得分:1)

CMake变量区分大小写。根据{{​​3}},您需要Boost_LIBRARIES,而不是BOOST_LIBRARIES

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