Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
758 views
in Technique[技术] by (71.8m points)

c++ - Error linking Boost with CMake

I am using CLion on OS X 10.10.5 (Yosemite) and trying to use the logging boost (1.58.0) library.

I installed boost using brew install boost. I checked that the boost libraries are 64-bit and so is my executable.

My CMakeLists.txt looks like this:

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})

My main.cpp looks like this:

#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;
}

I get the following output when building:

/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

How can I link to the boost libraries?

EDIT 1:

Here are the BOOST variable values from CMake:

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

Therefore it actually finds the libraries...

EDIT 2:

If I do $file /usr/local/lib/libboost_log-mt.dylib, I get

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

So this library is an x86_64 library, which is correct because I am also building my app as a 64 bit app.

EDIT 3:

I checked that boost is built against the libc++ library, and not libstdc++. This seems to be the default now when installing with homebrew. So this should not be a problem. I noticed some older discussions regarding this issue on older versions of OS X, but this does not seem to be an issue anymore.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to set #define BOOST_LOG_DYN_LINK 1, this can be done with cmake using add_definitions(-DBOOST_LOG_DYN_LINK=1).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...