CMake和Boost

Nem*_*vić 5 boost cmake boost-thread

我搜索并发现很多人都有同样的问题,但没有解决方案.

我正在使用CMake为MinGW生成Makefile,在编译时我收到错误:

CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x5e): undefined reference to `_imp___ZN5boost6thread4joinEv'
CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x71): undefined reference to `_imp___ZN5boost6threadD1Ev'
CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x88): undefined reference to `_imp___ZN5boost6threadD1Ev'
Run Code Online (Sandbox Code Playgroud)

这似乎是一个链接问题,我明白了.我的CMake配置是:

project(boosttest)
cmake_minimum_required(VERSION 2.6)

include_directories(${boosttest_SOURCE_DIR}/include c:/boost_1_48_0/)
link_directories(c:/boost_1_48_0/lib)

file(GLOB_RECURSE cppFiles src/*.cpp)

add_executable(boosttest ${cppFiles})

target_link_libraries(boosttest libboost_thread-mgw46-mt-1_48.a)
Run Code Online (Sandbox Code Playgroud)

首先我尝试使用find_package(Boost COMPONENTS thread)它,它的工作方式相同,所以我想尝试手动执行此操作,但仍然会遇到相同的错误.

有什么见解吗?

我使用bjam编译它为mingw并作为静态链接.还试过做:

add_library(imp_libboost_thread STATIC IMPORTED)
set_property(TARGET imp_libboost_thread PROPERTY IMPORTED_LOCATION c:/boost_1_48_0/lib/libboost_thread-mgw46-mt-1_48.a)
target_link_libraries(boosttest imp_libboost_thread)
Run Code Online (Sandbox Code Playgroud)

我仍然得到相同的错误消息.

Ale*_*xey 10

对于mingw32,您可以添加定义BOOST_THREAD_USE_LIB.与boost :: thread链接将起作用.你也可能需要Threads包(但我不确定,可能只需要*nix平台).

这是我的CMakeLists的一部分.我从项目中复制它,它使用boost :: thread,并在mingw-gcc(和其他编译器)下编译:

    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}
    )
Run Code Online (Sandbox Code Playgroud)