0

I am trying to use boost in my c++ project in macOS Sierra. I've installed boost using home-brew, like so:

brew install boost

My CMakeLists.txt file looks like cmake_minimum_required(VERSION 3.8) project(sr)

set(CMAKE_CXX_STANDARD 14)

set(Boost_USE_STATIC_LIBS        OFF) # only find static libs
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME     OFF)
find_package(Boost 1.64.0 COMPONENTS system log log_setup thread date_time filesystem regex chrono atomic REQUIRED)

include_directories(src)
include_directories(lib/easylogging++)
include_directories(${Boost_INCLUDE_DIRS})

add_subdirectory(src)
add_subdirectory(sr_tests)

set(SOURCE_FILES main.cpp lib/easylogging++/easylogging++.cc)

add_executable(sr_out ${SOURCE_FILES})
target_link_libraries(sr_out ${Boost_LIBRARIES} sr)

My main.cpp is simply #include

int main() {
    BOOST_LOG_TRIVIAL(info) << "hi";
    return 1;
}

In the build phase, the binary fails to link with

➜  build git:(master) ✗ cmake ..
-- The C compiler identification is AppleClang 8.1.0.8020042
-- The CXX compiler identification is AppleClang 8.1.0.8020042
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE
-- Boost version: 1.64.0
-- Found the following Boost libraries:
--   system
--   log
--   log_setup
--   thread
--   date_time
--   filesystem
--   regex
--   chrono
--   atomic
-- Configuring done
-- Generating done
-- Build files have been written to: /Volumes/sr/sr/build
➜  build git:(master) ✗ make VERBOSE=1
/usr/local/Cellar/cmake/3.8.2/bin/cmake -H/Volumes/sr/sr -B/Volumes/sr/sr/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.8.2/bin/cmake -E cmake_progress_start /Volumes/sr/sr/build/CMakeFiles /Volumes/sr/sr/build/CMakeFiles/progress.marks
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/Makefile2 all
/Library/Developer/CommandLineTools/usr/bin/make -f src/CMakeFiles/sr.dir/build.make src/CMakeFiles/sr.dir/depend
cd /Volumes/sr/sr/build && /usr/local/Cellar/cmake/3.8.2/bin/cmake -E cmake_depends "Unix Makefiles" /Volumes/sr/sr /Volumes/sr/sr/src /Volumes/sr/sr/build /Volumes/sr/sr/build/src /Volumes/sr/sr/build/src/CMakeFiles/sr.dir/DependInfo.cmake --color=
/Library/Developer/CommandLineTools/usr/bin/make -f src/CMakeFiles/sr.dir/build.make src/CMakeFiles/sr.dir/build
make[2]: Nothing to be done for `src/CMakeFiles/sr.dir/build'.
[ 30%] Built target sr
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/sr_out.dir/build.make CMakeFiles/sr_out.dir/depend
cd /Volumes/sr/sr/build && /usr/local/Cellar/cmake/3.8.2/bin/cmake -E cmake_depends "Unix Makefiles" /Volumes/sr/sr /Volumes/sr/sr /Volumes/sr/sr/build /Volumes/sr/sr/build /Volumes/sr/sr/build/CMakeFiles/sr_out.dir/DependInfo.cmake --color=
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/sr_out.dir/build.make CMakeFiles/sr_out.dir/build
[ 40%] Linking CXX executable sr_out
/usr/local/Cellar/cmake/3.8.2/bin/cmake -E cmake_link_script CMakeFiles/sr_out.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/c++   -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/sr_out.dir/main.cpp.o CMakeFiles/sr_out.dir/lib/easylogging++/easylogging++.cc.o  -o sr_out /usr/local/lib/libboost_system-mt.dylib /usr/local/lib/libboost_log-mt.dylib /usr/local/lib/libboost_log_setup-mt.dylib /usr/local/lib/libboost_thread-mt.dylib /usr/local/lib/libboost_date_time-mt.dylib /usr/local/lib/libboost_filesystem-mt.dylib /usr/local/lib/libboost_regex-mt.dylib /usr/local/lib/libboost_chrono-mt.dylib /usr/local/lib/libboost_atomic-mt.dylib src/libsr.a
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(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::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::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[2]: *** [sr_out] Error 1
make[1]: *** [CMakeFiles/sr_out.dir/all] Error 2
make: *** [all] Error 2
➜  build git:(master) ✗

I would really appreciate any help in getting this up and running.

gurjeet
  • 129
  • 6
  • Are you sure you're building a 64-bit executable? – tadman Jul 10 '17 at 02:15
  • I think that's what my cmake config does by default. BTW, removing boost from cmake and that one line of code in main.cpp makes the entire codebase compile a 64 bit binary just fine. – gurjeet Jul 10 '17 at 02:21
  • What symbols aren't found? – tadman Jul 10 '17 at 02:22
  • If it's important please put it in the body of the question. Off-site links are often ignored. Adding it to comments is even more messy. – tadman Jul 10 '17 at 02:24
  • OK, thanks! I've edited it – gurjeet Jul 10 '17 at 02:30
  • There we go. That's a lot easier to read. – tadman Jul 10 '17 at 02:31
  • Are you sure the Boost shared libraries are being linked in? I'd check the exact command executed for `ld` to be sure they are, as there's a chance you're referencing the wrong variable or something. – tadman Jul 10 '17 at 02:32
  • I am not sure, is there a way to check it? – gurjeet Jul 10 '17 at 02:33
  • Does Cmake have a detailed log you can check? – tadman Jul 10 '17 at 02:33
  • I've edited the note with the exact command being invoked and it looks correct to me. – gurjeet Jul 10 '17 at 02:36
  • Are the Boost logging functions in a different library for some reason? Just a dumb question since I think you've covered all the other bases. – tadman Jul 10 '17 at 02:42
  • I've been scratching my head for a day. My hunch is that the boost installed by brew is linked against a different libc++ while CMake is picking up clang, which is libstdc++ But I am not sure what to do about it! – gurjeet Jul 10 '17 at 02:46
  • Those are all prefixed with `boost::log`. Have you tried another test program with some other Boost thing involved, not the logging? – tadman Jul 10 '17 at 02:48
  • FIXED! The solution was at https://stackoverflow.com/questions/32270246/error-linking-boost-with-cmake – gurjeet Jul 10 '17 at 02:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148745/discussion-between-gurjeet-and-tadman). – gurjeet Jul 10 '17 at 03:06

1 Answers1

2

The problem was that the Boost Logging library had to be dynamically linked. I fixed my CMakeLists.txt file to add this line:

add_definitions(-DBOOST_LOG_DYN_LINK=1)

This fixed the issue

gurjeet
  • 129
  • 6