Linking Boost thread library

前端 未结 2 1120
无人及你
无人及你 2021-01-15 11:43

I am trying to include Boost\'s thread library in my C++ project. My CMake file is like so:

cmake_minimum_required(VERSION 3.6)
project(LearningC)

find_pack         


        
相关标签:
2条回答
  • Ok, I see you've found a solution, but there are some improvements I'd like to propose (as soon as you require CMake 3.6):

    • use imported targets to manage compiler/linker options per target, instead of "global" variables and functions (like include_directories(), ...)
    • use full signature of project() to define a bunch of PROJECT_xxx variables, then use them
    • provide explicit list of languages do you use to avoid default and possible redundant checks
    • in case of Boost, you don't need to find and link with implicit dependencies -- FindBoost.cmake would do it for you. Just specify components only what you really need and use.

    Here is a modified CMakeLists.txt:

    cmake_minimum_required(VERSION 3.6)
    project(LearningCxx VERSION 1.0.0 LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Boost COMPONENTS thread REQUIRED)
    
    add_executable(
        ${PROJECT_NAME}
        main.cpp
        Student.cpp
      )
    
    target_link_libraries(
        ${PROJECT_NAME}
        Boost::thread
      )
    

    Update: I've done an example for this project, but remove Students.cpp and replace main.cpp with the following code:

    #include <boost/thread.hpp>
    #include <iostream>
    
    int main()
    {
        std::cout << boost::thread::hardware_concurrency() << std::endl;
    }
    

    Here is my test run:

    zaufi@gentop⟩…/tests/boost-thread-cmake/build⟩empty dir⟩cmake ..
    -- The CXX compiler identification is GNU 5.4.0
    -- Check for working CXX compiler: /usr/lib/outproc/bin/c++ -- works
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features - done
    -- Looking for C++ include pthread.h - found
    -- Looking for pthread_create - not found
    -- Looking for pthread_create in pthreads - not found
    -- Looking for pthread_create in pthread - found
    -- Found Threads: TRUE
    -- Boost version: 1.62.0
    -- Found the following Boost libraries:
    --   thread
    --   chrono
    --   system
    --   date_time
    --   atomic
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /work/tests/boost-thread-cmake/build
    zaufi@gentop⟩…/tests/boost-thread-cmake/build⟩default⟩pfx: /usr/local⟩make
    Scanning dependencies of target LearningCxx
    [ 50%] Building CXX object CMakeFiles/LearningCxx.dir/main.cpp.o
    [100%] Linking CXX executable LearningCxx
    [100%] Built target LearningCxx
    zaufi@gentop⟩…/tests/boost-thread-cmake/build⟩default⟩pfx: /usr/local⟩ldd ./LearningCxx
            linux-vdso.so.1 (0x00007fff73b75000)
            libboost_thread.so.1.62.0 => /usr/lib64/libboost_thread.so.1.62.0 (0x00007fd1adafd000)
            libboost_chrono.so.1.62.0 => /usr/lib64/libboost_chrono.so.1.62.0 (0x00007fd1ad8f6000)
            libboost_system.so.1.62.0 => /usr/lib64/libboost_system.so.1.62.0 (0x00007fd1ad6f2000)
            libboost_date_time.so.1.62.0 => /usr/lib64/libboost_date_time.so.1.62.0 (0x00007fd1ad4e1000)
            libboost_atomic.so.1.62.0 => /usr/lib64/libboost_atomic.so.1.62.0 (0x00007fd1ad2df000)
            libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd1ad0c3000)
            libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/6.3.0/libstdc++.so.6 (0x00007fd1accc8000)
            libm.so.6 => /lib64/libm.so.6 (0x00007fd1ac9cd000)
            libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/6.3.0/libgcc_s.so.1 (0x00007fd1ac7b6000)
            libc.so.6 => /lib64/libc.so.6 (0x00007fd1ac41d000)
            librt.so.1 => /lib64/librt.so.1 (0x00007fd1ac215000)
            /lib64/ld-linux-x86-64.so.2 (0x00007fd1add25000)
    zaufi@gentop⟩…/tests/boost-thread-cmake/build⟩default⟩pfx: /usr/local⟩cmake --version
    cmake version 3.7.1
    
    0 讨论(0)
  • 2021-01-15 12:25

    I found a solution. Basically, Boost has most of its code in C++ headers (.hpp). Some of the libraries however need to be compiled and linked... The code below works!

    cmake_minimum_required(VERSION 3.6)
    project(LearningC)
    
    set(CMAKE_CXX_STANDARD 11)
    
    set(SOURCE_FILES main.cpp Student.cpp Student.h)
    
    add_executable(LearningC ${SOURCE_FILES})
    
    find_package(Boost COMPONENTS thread system REQUIRED)
    
    include_directories(${Boost_INCLUDE_DIR})
    target_link_libraries(LearningC ${Boost_LIBRARIES})
    
    0 讨论(0)
提交回复
热议问题