CMake and Boost

后端 未结 2 1202
[愿得一人]
[愿得一人] 2020-12-29 15:57

I\'ve searched and found out that a lot of people have the same problem, but no solution exists.

I\'m using CMake to generate Makefiles for MinGW and when compiling

相关标签:
2条回答
  • 2020-12-29 16:43

    For mingw32 you may add definition BOOST_THREAD_USE_LIB. And linking with boost::thread will work. Also you may need Threads package (but i'm not sure, may be it needs only for *nix platforms).

    Here is part of my CMakeLists. I copied it from project, which uses boost::thread, and compiles under mingw-gcc (and other compilers):

        set(Boost_USE_STATIC_LIBS   ON)
        set(Boost_USE_MULTITHREADED ON)
        set(Boost_ADDITIONAL_VERSIONS "1.44" "1.44.0")
        find_package(Boost COMPONENTS thread date_time program_options filesystem system REQUIRED)
        include_directories(${Boost_INCLUDE_DIRS})
    
        find_package(Threads REQUIRED)
    
        #...
    
        if (WIN32 AND __COMPILER_GNU)
            # mingw-gcc fails to link boost::thread
            add_definitions(-DBOOST_THREAD_USE_LIB)
        endif (WIN32 AND __COMPILER_GNU)
    
        #...
    
        target_link_libraries(my_exe
                ${CMAKE_THREAD_LIBS_INIT}
                #...
            ${Boost_LIBRARIES}
        )
    
    0 讨论(0)
  • 2020-12-29 16:59

    In my opinion, this question is similar to this question and this one. My best guess would be that you need the same resolution as in my answer to the first question.

    I would strongly recommend the use of find_package (Boost ) and take care with the auto-linking:

    project(boosttest)
    cmake_minimum_required(VERSION 2.6)
    
    # Play with the following defines
    # Disable auto-linking. 
    add_definition( -DBOOST_ALL_NO_LIB )
    # In case of a Shared Boost install (dlls), you should then enable this
    # add_definitions( -DBOOST_ALL_DYN_LINK )
    
    # Explicitly tell find-package to search for Static Boost libs (if needed)
    set( Boost_USE_STATIC_LIBS ON ) 
    find_package( Boost REQUIRED COMPONENTS thread )
    
    include_directories( ${Boost_INCLUDE_DIRS} )
    
    file(GLOB_RECURSE cppFiles src/*.cpp)
    
    add_executable(boosttest ${cppFiles})
    
    target_link_libraries(boosttest ${Boost_LIBRARIES} )
    
    0 讨论(0)
提交回复
热议问题