4

Yet another one issue with linking

OS X 10.9.5 x64

The cross-platform project is being built using CMake toolchain. Uses boost.

Error linking all of executables with Boost Log (BOOST_PATH is definitely found, unit-tests link with Boost Test successfuly)

First try. Using Clang

gcc -v
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

Building boost

./bootstrap &&  ./b2 threading=multi link=static runtime-link=static install

Completed successfully

...updated 638 targets...

Linking instructions:

if( ("UNIX") AND ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") )

    # using Clang
    message("UNIX configuration, Clang")
    set(CMAKE_CXX_FLAGS "-std=c++11")
    set(CMAKE_EXE_LINKER_FLAGS "-std=c++11 -Wl")

Linking libraries to executable

add_executable(${TARGET} ${SOURCES})
if( ("UNIX") AND ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") )

    # using Clang
    message("UNIX configuration, Clang")
    target_link_libraries(${TARGET}
        libhelpers.a
        libopenvpn_manager.a
        ${Boost_LOG_LIBRARY}
        ${Boost_LOG_SETUP_LIBRARY}
        ${Boost_SYSTEM_LIBRARY}
        ${Boost_FILESYSTEM_LIBRARY})

Generating makefiles:

cmake -DCMAKE_OSX_ARCHITECTURES=x86_64 -DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++exit .

Linker output (some lines selected):

Undefined symbols for architecture x86_64:
 "boost::detail::get_tss_data(void const*)", referenced from:
      boost::log::v2s_mt_posix::core::add_thread_attribute(boost::log::v2s_mt_posix::attribute_name const&, boost::log::v2s_mt_posix::attribute const&) in libboost_log.a(core.o)
      boost::log::v2s_mt_posix::core::remove_thread_attribute(boost::log::v2s_mt_posix::attribute_set::iter<false>) in libboost_log.a(core.o)
      boost::log::v2s_mt_posix::core::get_thread_attributes() const in libboost_log.a(core.o)
      ...
  "boost::detail::set_tss_data(void const*, boost::shared_ptr<boost::detail::tss_cleanup_function>, void*, bool)", referenced from:
      boost::thread_specific_ptr<boost::log::v2s_mt_posix::core::implementation::thread_data>::reset(boost::log::v2s_mt_posix::core::implementation::thread_data*) in libboost_log.a(core.o)

      ...
  "boost::detail::thread_data_base::~thread_data_base()", referenced from:
      boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::~thread_data() in openvpn_test_app.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)

Second try. Using GCC

How I made it default

#!/bin/bash
cd /usr/bin
rm cc gcc c++ g++
ln -s /usr/local/bin/gcc-4.9 cc
ln -s /usr/local/bin/gcc-4.9 gcc
ln -s /usr/local/bin/c++-4.9 c++
ln -s /usr/local/bin/g++-4.9 g++

gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.2.0/4.9.1/lto-wrapper
Target: x86_64-apple-darwin13.2.0
Configured with: ../configure --build=x86_64-apple-darwin13.2.0 --prefix=/usr/local/Cellar/gcc/4.9.1 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-4.9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-cloog=/usr/local/opt/cloog --with-isl=/usr/local/opt/isl --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --with-pkgversion='Homebrew gcc 4.9.1' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --enable-multilib
Thread model: posix
gcc version 4.9.1 (Homebrew gcc 4.9.1)

Build Boost the same way as previous

Linking instructions

elseif( ("UNIX") AND ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") )

    # using GCC
    message("UNIX congiguration, GCC")
    set(CMAKE_CXX_FLAGS "-std=c++11 -pthread")
    set(CMAKE_EXE_LINKER_FLAGS "-std=c++11 -pthread")

Linking libraries to executable same way as with Clang

target_link_libraries(${TARGET}
    libhelpers.a
    libopenvpn_manager.a
    ${Boost_LOG_LIBRARY}
    ${Boost_LOG_SETUP_LIBRARY}
    ${Boost_SYSTEM_LIBRARY}
    ${Boost_FILESYSTEM_LIBRARY})

Generating makefiles

cmake -DCMAKE_OSX_ARCHITECTURES=x86_64 -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ .

Linker output:

Undefined symbols for architecture x86_64:
"boost::detail::get_tss_data(void const*)", referenced from:
      boost::log::v2s_mt_posix::core::set_thread_attributes(boost::log::v2s_mt_posix::attribute_set const&) in libboost_log.a(core.o)
boost::log::v2s_mt_posix::core::get_thread_attributes() const in libboost_log.a(core.o)
      boost::log::v2s_mt_posix::core::remove_thread_attribute(boost::log::v2s_mt_posix::attribute_set::iter<false>) in libboost_log.a(core.o)
…
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

2 Answers2

6

Your missing symbols are from Boost.Thread, which is used by Boost.Log in multi-threaded configuration. You need to add "thread" component to your find_package line and the Boost_THREAD_LIBRARY variable to the target_link_libraries line. Note that you may need to ensure the correct order of libraries in the target_link_libraries line to avoid linking errors caused by symbol dependency resolution order (leaf dependencies should be specified last). For example:

find_package(Boost COMPONENTS log log_setup thread filesystem system)
target_link_libraries(${TARGET}
    ${Boost_LOG_SETUP_LIBRARY}
    ${Boost_LOG_LIBRARY}
    ${Boost_FILESYSTEM_LIBRARY}
    ${Boost_THREAD_LIBRARY}
    ${Boost_SYSTEM_LIBRARY}
)

Depending on the features you use you may need to similarly add linking with Boost.Regex and Boost.DateTime.

Andrey Semashev
  • 10,046
  • 1
  • 17
  • 27
  • I needed to add: `set_target_properties(${TARGET} PROPERTIES COMPILE_DEFINITIONS "BOOST_LOG_DYN_LINK")` – malat Jul 02 '15 at 07:45
0

I don't see

find_package(Boost COMPONENTS filesystem system .... etc... )  

anywhere in your example. You will need that in order to have Boost_FILESYSTEM_LIBRARY defined, for example. You can use the CMake command

message("Boost_FILESYSTEM_LIBRARY: ${Boost_FILESYSTEM_LIBRARY}")  

to see the current value of the Boost_FILESYSTEM_LIBRARY. All of those library names are set in the findBoost.cmake module.

StAlphonzo
  • 736
  • 5
  • 14
  • 1
    This gives me an error: `Cannot specify link libraries for target "/usr/local/lib/libboost_log_setup-mt.dylib" which is not built by this project.` – IgorGanapolsky May 26 '16 at 21:18
  • 1
    Does this work, getting the same error in the comment. @IgorGanapolsky – Piyush Chauhan Jun 19 '16 at 21:38
  • My example, included Boost's filesystem library only. If you want other libraries, you can add more components to the find_package list (see answer above). If you want everything, then don't add the COMPONENTS directive and link ${Boost_LIBRARIES} instead. – StAlphonzo Jun 21 '16 at 11:30