17

I am trying to get a simple boost.log example running on Linux using GCC 4.4.5, CMake 2.8.2 and Boost 1.53.0.

Compiling boost and boost log succeeded, but I keep getting issues when linking my test program to boost.log.

I use the following CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8)

project(QuantibBoostLogTest)

# Include boost headers
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Threads)
find_package(Boost 1.53.0 COMPONENTS thread date_time filesystem system log log_setup REQUIRED)
if(Boost_FOUND)
  include_directories( ${Boost_INCLUDE_DIRS} )
  link_libraries(${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})
else(Boost_FOUND)
  message(FATAL_ERROR "Cannot build Quantib Boost Log test without Boost. Please set Boost_DIR.")
endif(Boost_FOUND)

add_executable(quantibBoostLogTest boost_log_test.cxx)
install(TARGETS quantibBoostLogTest DESTINATION .)

CMake does detect the boost libraries correctly, but I still get linker errors, mostly of the form:

core.cpp:(.text+0x1b0e): undefined reference to `boost::detail::get_tss_data(void const*)'

I do link the thread libraries. Does anybody know how to solve this?

Coert Metz
  • 894
  • 6
  • 21

2 Answers2

22

It seems like boost.log depends on boost.thread library then you need change order of libraries. See why link order does matter

Try following order

find_package(Boost 1.53.0 COMPONENTS log log_setup thread date_time filesystem system REQUIRED)

if it will not help try include them two times as following

link_libraries(${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${Boost_LIBRARIES})
Community
  • 1
  • 1
Nikolay Viskov
  • 1,016
  • 6
  • 9
2

The linker error you give has something to do with either not linking against a native threading library like pthreads and/or boost_thread. (or both)

1) From what I see you don't link against pthreads library.
By merely calling a CMake custom module that tries to find the library doesn't mean it'll also link against it.

Try and do:

SET(CMAKE_THREAD_PREFER_PTHREAD true)
FIND_PACKAGE (Threads)
IF(Threads_FOUND)
INCLUDE_DIRECTORIES(SYSTEM ${Threads_INCLUDE_DIR})
MESSAGE("Are we using pthreads? ${CMAKE_USE_PTHREADS_INIT}")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})
ENDIF()

Check the FindThreads.cmake file of the CMake installation you have for more information regarding the use of the threads module. You can usually find it in /usr/share/cmake-2.8/Modules/

2) Maybe the ordering of the linked Boost libraries is incorrect or the version you specified for Boost is invalid.

Try changing the boost version or don't specify it at all or change the order of the linked libraries

SET(Boost_USE_STATIC_LIBS ON)  
SET(Boost_USE_MULTITHREADED ON)  
FIND_PACKAGE(Boost 1.53.0 COMPONENTS **system thread filesystem date_time log log_setup** REQUIRED)  

IF(Boost_FOUND)  
    INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIR})  
    LINK_DIRECTORIES(${Boost_LIBRARY_DIR})  
    MESSAGE("Boost information")  
    MESSAGE("Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")  
    MESSAGE("Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")  
    MESSAGE("Boost Libraries: ${Boost_LIBRARIES}")  
    TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Boost_LIBRARIES})  
ENDIF()

(The second contention might be completely wrong as I think the ordering of the elements specified after COMPONENTS in FIND_PACKAGE doesn't matter)

Alex Bitek
  • 6,529
  • 5
  • 47
  • 77
  • Thanks for you suggestions! I tried both, bot neither had a positive effect. The Boost include and library dir output of CMake is correct. The Boost_LIBRARIES also seem to be correct. optimized;/home/c00/boost/boost_1_53_0_install/lib/libboost_system-mt.a;debug;/home/c00/boost/boost_1_53_0_install/lib/libboost_system-mt-d.a; [etc ...] Output of threads: Are we using pthreads? 1. Threads link tag: -lpthread – Coert Metz May 14 '13 at 11:42
  • @CoertMetz As I see that you're using a non system wide Boost installation, did you set BOOST_ROOT variable to the correct path SET(BOOST_ROOT h‌​ome/c00/boost/boost_1_53_0_install) before FIND_PACKAGE(Boost 1.53.0... ? Have you also tried removing the 1.53.0 from FIND_PACKAGE? Also try to run **make VERBOSE=1** or **SET(CMAKE_VERBOSE_MAKEFILE ON)** in CMakeLists.txt – Alex Bitek May 14 '13 at 13:10
  • I think there might also be a mismatch between the headers of Boost you are using and the Boost libraries you are linking against, and if you have both a system wide Boost installation and a local one in your home directory, CMake might get confused. **Double check that you're linking against the right boost_thread (-mt or not) and pthread** – Alex Bitek May 14 '13 at 13:24
  • A mismatch between the headers and libraries might indeed be the problem. How can I make sure that gcc is using my own 1.53.0 headers instead of the system wide headers? – Coert Metz May 14 '13 at 14:17
  • The C++ command has: -isystem /home/c00/boost/boost_1_53_0_install/include, so that should be ok or not? And how do I know what the right pthread is? – Coert Metz May 14 '13 at 14:19
  • @CoertMetz The MESSAGE calls I put in my answer should tell you all you need to know about which headers and which static libraries are used. As for pthreads I don't think you need to worry about it, except that you have to ensure that you're linking against it. TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT}) I don't understand how my code sample doesn't work for you... I have the exact same code in my project and it finds the correct Boost in my home directory after I've SET(BOOST_ROOT path/to/boost/root/dir) – Alex Bitek May 15 '13 at 06:33