Hi, I had some problems with linking Boost.Process. Linking with the Boost::boost
target after using find_package(Boost)
gave me linking errors about threads. After some discussion on IRC, the problem is that Boost being a header only library, it doesn’t link with threads. So, ideally there would be a Boost::process
target, which would link with threads. Could that be added to CMake? I’d be willing to implement that myself (but I’m not insisting on that).
I’ll leave the original post here for more info and a reproducer:
Original post
Boost.Process doesn’t link with Threads automatically
Hi. I have this source file which only includes Boost.Process:
#include <boost/process.hpp>
int main(int argc, char* argv[])
{
return 0;
}
And this CMakeLists.txt:
project(main)
cmake_minimum_required(VERSION 3.18)
find_package(Boost REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main Boost::boost)
Upon running and building I get these linking errors:
$ cmake .. && make
-- The C compiler identification is GNU 10.2.0
-- The CXX compiler identification is GNU 10.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /usr/lib64/cmake/Boost-1.72.0/BoostConfig.cmake (found version "1.72.0")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/vk/test/build
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
/usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `boost::asio::detail::posix_thread::~posix_thread()':
main.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to `pthread_detach'
/usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `boost::asio::detail::posix_thread::join()':
main.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[_ZN5boost4asio6detail12posix_thread4joinEv]+0x2b): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:103: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
I have to manually find the thread package and add it to the target_link_libraries
directive like so:
find_package(Threads)
target_link_libraries(main Boost::boost Threads::Threads)
My question is, is this a bug? I think if Boost.Process uses threads, then cmake should link with them by default. Also it’s kind of weird that just including and not using it requires Thread linking, but that’s probably on Boost.Process
implementation. My version of CMake is 3.18.4 and I use Arch Linux.
Thanks.