Boost::process target doesn't exist (for thread linking)

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:

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 - 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:

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.


This line suggests that Boost targets have been defined by Boost-provided config file. For me it looks like bug on the Boost side.

I was looking at this piece of CMake code and I just thought that it was maybe CMake’s responsibility to add the special targets (and link their dependencies):

I don’t understand CMake enough to know how find_package works, but looking at CMake’s output with the --debug-find flag, I can see that this FindBoost module is being used at least in some way. The log is here:

Yep, it’s using the configuration provided by the package (this section: )

In that case BoostConfig.cmake is responsible for setting up the targets.
From the other hand there seems to be no target Boost::process and you’re only using the generic Boost::boost. It may be done on purpose as to not bring a dependency when one may not be needed and documented somewhere.

The reason is quite simple:

Boost.Process is a header-only library. For header-only libraries BoostConfig.cmake only defines the Boost::headers target (and its backward-compatible alias-target Boost::boost). Only non-header-only Boost libraries, that have a static/shared library file, have a specific CMake target. (Only exception I know of is Boost.Exception.)

This might change in the future. (In fact, I hope it will! That would be clearer and allow better modularization of Boost.)

And as FindBoost is mimicking BoostConfig.cmake's behavior it also only provides the same CMake targets.

1 Like

So bottom line - for the time being one has to know if certain Boost header-only libraries require additional dependencies and link those oneself. A bit unfortunate but also not that tragic :wink:

Okay, so, for the time being, I’ll link threads manually. Hopefully, there can be a target for Boost::process in the future.

Thank your for your help!