Home CMake build fails unless pthread is called on the target_link_directories() command
Reply: 0

CMake build fails unless pthread is called on the target_link_directories() command

user3701 Published in March 24, 2018, 5:28 pm

I have a linking issue with CMake 3.10 and Boost 1_66_0. I'm using the asio async timer tutorial for testing as I prepare to do some networking. I'm on a bleeding edge Linux maching working on a project that required me to install Boost into a custom directory:


I set the following environment variables in my .bash_profile:

export BOOST_ROOT=/home/myuser/boost/boost_1_66_0
export BOOST_LIBRARYDIR=/home/myuser/boost/boost_1_66_0/stage/lib

Although I managed to get this working, the build fails unless pthread is called on the target_link_libraries() command, even though I am calling Boost's own thread library on the find_package() command.

I did not find any mention of the need to call pthread in Boost's getting started guide, or in CMake's documentation.

Here is my full CMakeLists.txt file:

  1 cmake_minimum_required(VERSION 3.0)
  2 project(asio_tut)
  3 set(Boost_DEBUG ON)
  6   set(CMAKE_INSTALL_PREFIX=/home/myuser/projects/asio_tut/build CACHE PATH test FORCE)
  9 find_package(Boost REQUIRED COMPONENTS system thread)
 11 if(Boost_FOUND)
 12   include_directories(${Boost_INCLUDE_DIR})
 13   add_executable(asio_tut timer_async.cpp)
 14   target_link_libraries(asio_tut ${Boost_LIBRARIES})
 15 endif()

CMake finds the thread library:

-- [ /home/myuser/builds/cmake/share/cmake-3.10/Modules/FindBoost.cmake:1767 ] Boost_FOUND = 1
-- Boost version: 1.66.0
-- Found the following Boost libraries:
--   system
--   thread
--   chrono
--   date_time
--   atomic
-- Configuring done
-- Generating done
-- Build files have been written to: /home/myuser/projects/asio_tut/build

But then it fails on the proceeding make command as it insists on pthread:

[myuser@linux build]$ make
Scanning dependencies of target asio_tut
[ 50%] Building CXX object CMakeFiles/asio_tut.dir/timer_async.cpp.o
[100%] Linking CXX executable asio_tut
/usr/bin/ld: CMakeFiles/asio_tut.dir/timer_async.cpp.o: undefined reference to symbol 'pthread_condattr_setclock@@GLIBC_2.3.3'
/usr/lib/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/asio_tut.dir/build.make:100: asio_tut] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/asio_tut.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

To fix this I had to add pthread to the targe_link_libraries() command:

target_link_libraries(asio_tut ${Boost_LIBRARIES} pthread)

Is this normal? Will this come back to haunt me later on? Will it cause any problems with portability? Should I just ignore it?

My CMakeCache.txt file shows that CMake found all of my Boost libraries and headers in the custom directories. I won't include the entire cache file, but I examined the cache entries and they are correct.

Side Point

Not sure if this is related but I did get one warning during the CMake build about my Boost version as it is bleeding edge:

CMake Warning at /home/myuser/builds/cmake/share/cmake-3.10/Modules/FindBoost.cmake:801 (message):
  New Boost version may have incorrect or missing dependencies and imported
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.394933 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO