2012-02-13 104 views
5

我查过并发现很多人都有同样的问题,但没有解决方案。CMake和Boost

我使用的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),它正在以同样的方式,所以我想尝试手动做到这一点,我仍然得到同样的错误。

对此有何见解?

我已经编译它为mingw使用bjam和作为一个静态链接。也试过:

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) 

而且我仍然得到相同的错误消息。

回答

10

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

这是我的CMakeLists的一部分。我复制它从项目,该项目采用的boost ::线程,并且在MinGW的海湾合作委员会(和其他编译器)编译:

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

添加BOOST_THREAD_USE_LIB为我修好了。在过去的一个小时左右,我一直在为这件事挠头。 – CadentOrange 2012-05-09 09:33:30

3

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

我会强烈建议使用find_package(升压),并照顾与自动链接:

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

我用尽了一切办法从那些帖子了,但是现在还复查一切,所有的时间我犯了同样的错误。它确实找到了库并完全设置了包含,但不知何故它不会链接......我还使用'--build-type = complete'重新编译了boost,并尝试进行动态链接,并且它仍然获胜't work ... – 2012-02-13 13:00:06

+0

在我的情况下,我必须添加'find_package(Boost COMPONENTS thread system REQUIRED)'和'target_link_libraries( $ {Boost_LIBRARIES})''当然! – Tanasis 2017-10-03 10:19:40