【问题标题】:CMake and BoostCMake 和 Boost
【发布时间】:2012-03-04 12:24:02
【问题描述】:

我搜索了很多人都有同样的问题,但没有解决方案。

我正在使用 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'

这似乎是一个链接问题,我明白了。我的 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)

首先我尝试使用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)

我仍然收到相同的错误消息。

【问题讨论】:

    标签: boost cmake boost-thread


    【解决方案1】:

    对于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}
        )
    

    【讨论】:

    • 添加 BOOST_THREAD_USE_LIB 为我修复了它。在过去的一个小时左右,我一直在摸索这个问题。
    【解决方案2】:

    在我看来,这个问题类似于this questionthis one。我最好的猜测是您需要与我对first question 的回答相同的分辨率。

    我强烈建议使用 find_package (Boost) 并注意自动链接:

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

    【讨论】:

    • 我已经尝试了这些帖子中的所有内容,但现在也重新检查了所有内容,并且一直遇到同样的错误。它确实找到了库并完美地设置了包含,但不知何故它不会链接......我还使用--build-type=complete重新编译了boost并尝试进行动态链接,但它仍然无法工作......
    • 就我而言,我当然必须添加find_package(Boost COMPONENTS thread system REQUIRED)target_link_libraries(<target> ${Boost_LIBRARIES})
    猜你喜欢
    • 2013-05-08
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多