CMake and Boost

自作多情 提交于 2019-11-30 05:26:14

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}
    )
André

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} )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!