将Boost与CMake链接时出错

时间:2015-08-28 11:40:34

标签: c++ boost cmake clion

我在OS X 10.10.5(Yosemite)上使用CLion并尝试使用logging boost(1.58.0)库。

我使用brew install boost安装了boost。我检查了boost库是64位的,我的可执行文件也是如此。

我的CMakeLists.txt看起来像这样:

set(CMAKE_VERBOSE_MAKEFILE on)
cmake_minimum_required(VERSION 3.3)
project(gmch)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(Boost_USE_MULTITHREADED ON)
find_package(Boost 1.58.0 COMPONENTS log REQUIRED)

include_directories(${Boost_INCLUDE_DIR})


set(SOURCE_FILES main.cpp util/timing.hpp)
add_executable(gmch ${SOURCE_FILES})
target_link_libraries(gmch ${Boost_LIBRARIES})

我的main.cpp看起来像这样:

#include <iostream>

#include <boost/log/trivial.hpp>

int main() {
    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

构建时我得到以下输出:

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug --target gmch -- -j 4
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/iulian/ClionProjects/gmch -B/Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 gmch
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/iulian/ClionProjects/gmch -B/Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug/CMakeFiles 2
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 CMakeFiles/gmch.dir/all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/gmch.dir/build.make CMakeFiles/gmch.dir/depend
cd /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug && /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /Users/iulian/ClionProjects/gmch /Users/iulian/ClionProjects/gmch /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug/CMakeFiles/gmch.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/gmch.dir/build.make CMakeFiles/gmch.dir/build
[ 50%] Linking CXX executable gmch
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/gmch.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -std=c++11 -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/gmch.dir/main.cpp.o  -o gmch  
Undefined symbols for architecture x86_64:
  "boost::log::v2s_mt_posix::record_view::public_data::destroy(boost::log::v2s_mt_posix::record_view::public_data const*)", referenced from:
      boost::log::v2s_mt_posix::record::reset() in main.cpp.o
  "boost::log::v2s_mt_posix::aux::stream_provider<char>::release_compound(boost::log::v2s_mt_posix::aux::stream_provider<char>::stream_compound*)", referenced from:
      boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::auto_release::~auto_release() in main.cpp.o
  "boost::log::v2s_mt_posix::aux::stream_provider<char>::allocate_compound(boost::log::v2s_mt_posix::record&)", referenced from:
      boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::record_pump(boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>&, boost::log::v2s_mt_posix::record&) in main.cpp.o
  "boost::log::v2s_mt_posix::aux::unhandled_exception_count()", referenced from:
      boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::~record_pump() in main.cpp.o
      boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::record_pump(boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>&, boost::log::v2s_mt_posix::record&) in main.cpp.o
  "boost::log::v2s_mt_posix::core::open_record(boost::log::v2s_mt_posix::attribute_set const&)", referenced from:
      boost::log::v2s_mt_posix::record boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex> >::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> const&) in main.cpp.o
  "boost::log::v2s_mt_posix::core::push_record_move(boost::log::v2s_mt_posix::record&)", referenced from:
      boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex> >::push_record_unlocked(boost::log::v2s_mt_posix::record&&) in main.cpp.o
  "boost::log::v2s_mt_posix::sources::aux::get_severity_level()", referenced from:
      boost::log::v2s_mt_posix::sources::aux::severity_level<boost::log::v2s_mt_posix::trivial::severity_level>::set_value(boost::log::v2s_mt_posix::trivial::severity_level) in main.cpp.o
  "boost::log::v2s_mt_posix::trivial::logger::get()", referenced from:
      _main in main.cpp.o
  "boost::log::v2s_mt_posix::core::get_logging_enabled() const", referenced from:
      boost::log::v2s_mt_posix::record boost::log::v2s_mt_posix::sources::basic_composite_logger<char, boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex>, boost::log::v2s_mt_posix::sources::features<boost::log::v2s_mt_posix::sources::severity<boost::log::v2s_mt_posix::trivial::severity_level> > >::open_record<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> const&) in main.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[3]: *** [gmch] Error 1
make[2]: *** [CMakeFiles/gmch.dir/all] Error 2
make[1]: *** [CMakeFiles/gmch.dir/rule] Error 2
make: *** [gmch] Error 2

如何链接到boost库?

编辑1:

以下是来自CMake的BOOST变量值:

Boost Include: /usr/local/include
Boost Libraries: /usr/local/lib
Boost Libraries: /usr/local/lib/libboost_log-mt.dylib

因此它实际上找到了库......

编辑2:

如果我$file /usr/local/lib/libboost_log-mt.dylib,我会

/usr/local/lib/libboost_log-mt.dylib: Mach-O 64-bit dynamically linked shared library x86_64

所以这个库是一个x86_64库,这是正确的,因为我也将我的应用程序构建为64位应用程序。

编辑3:

我检查过boost是针对libc ++库而不是libstdc ++构建的。现在使用自制软件安装时,这似乎是默认设置。所以这应该不是问题。我注意到在旧版OS X上有关此问题的一些较旧的讨论,但这似乎不再是一个问题。

2 个答案:

答案 0 :(得分:3)

您必须设置#define BOOST_LOG_DYN_LINK 1,这可以通过使用add_definitions(-DBOOST_LOG_DYN_LINK=1)的cmake来完成。

答案 1 :(得分:1)

阅读输出使我怀疑更清楚,它没有与任何 Boost库链接,因为您还没有向find_package命令添加任何Boost库。

您需要在find_package命令中明确列出要查找和使用的库。