c++multithreadingcmakeboost

CMAKE does not find Boost system and thread


With my Boost installation being on D:\boost_1_83_0 and being specified by BOOST_ROOT in my Path-Variable (Users Path Variables), parts of the installation are not found by my mingw64 C++-compiler.

This confuses me, because i worked with the io_context and other elements of the library beforehand and it worked, but when i got to working with threads, i was receiving linker errors. parts of the thread library were not found:

D:/boost_1_83_0/boost/thread/detail/thread.hpp:182: undefined reference to `boost::thread::start_thread_noexcept()'

D:/boost_1_83_0/boost/thread/detail/thread.hpp:257: undefined reference to `boost::thread::detach()'

etc.

I tried to explicitly link the thread and system libraries, confirming beforehands that my installation was not missing them. I did so inside my CMAKE-File:

cmake_minimum_required(VERSION 3.26)
project(Boost_Asio_Tutorial)

set(CMAKE_CXX_STANDARD 17)

add_executable(Boost_Asio_Tutorial main.cpp)

find_package(Boost 1.83.0 REQUIRED COMPONENTS thread system)

add_link_options(
        "-municode"

        "-lboost_thread-mgw12-mt-1_83"
        "-lboost_thread"
        "-lboost_system"
        "-lpthread"

)

target_include_directories(${PROJECT_NAME}
        PUBLIC
        $<INSTALL_INTERFACE:include>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        ${Boost_INCLUDE_DIRS}
        PRIVATE
)

target_link_libraries(${PROJECT_NAME}
        PRIVATE
        ${Boost_LIBRARIES}
        PUBLIC
)

if(MINGW)
    target_link_libraries(${PROJECT_NAME}
            PUBLIC
            ws2_32
            wsock32
    )
endif()

this will produce the following error:

CMake Error at C:/Program Files/JetBrains/CLion 2023.2.1/bin/cmake/win/x64/share/cmake-3.26/Modules/FindPackageHandleStandardArgs.cmake:230 (message): Could NOT find Boost (missing: thread system) (found suitable version "1.83.0", minimum required is "1.83.0") Call Stack (most recent call first): C:/Program Files/JetBrains/CLion 2023.2.1/bin/cmake/win/x64/share/cmake-3.26/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE) C:/Program Files/JetBrains/CLion 2023.2.1/bin/cmake/win/x64/share/cmake-3.26/Modules/FindBoost.cmake:2377 (find_package_handle_standard_args) CMakeLists.txt:8 (find_package)

so what am i missing?

my cpp file looks like this:

#include <boost/bind/bind.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>

int main() {
    io::io_context ioContext;
    syncedPrinter sp(ioContext);
    boost::thread t(boost::bind(&boost::asio::io_context::run, &ioContext));
    ioContext.run();
    t.join();

    return 0;
}

Solution

  • While many of the libraries in boost are header only some of them like boost_thread will need to be compiled to a static or dynamic library (depending on your choice). You need to install boost binaries for your compiler MinGW. The boost.org download contains the source code and headers and scripts needed to build boost.

    The official documentation for how to build boost is here: https://www.boost.org/doc/libs/1_83_0/more/getting_started/windows.html#prepare-to-use-a-boost-library-binary

    Building could be as simple as opening a MinGW terminal with your compiler settings enabled and typing after you change directory to the boost root folder:

    bootstrap
    .\b2
    

    This question should help in the process of building boost: How to build Boost 1.64 in 64 bits?