2012-02-13 18 views
5

He buscado y descubrí que muchas personas tienen el mismo problema, pero no existe ninguna solución.CMake and Boost

estoy usando CMake para generar Makefile para compilar MinGW y cuando estoy consiguiendo un error:

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' 

Esto parece ser un problema de vinculación, lo entiendo. Mi configuración de CMake es:

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) 

Primero trató de usar find_package(Boost COMPONENTS thread) y que estaba trabajando de la misma manera, así que pensé que tratar de hacerlo de forma manual y sigo teniendo el mismo error.

¿Tiene alguna idea sobre esto?

Lo he compilado para mingw usando bjam y como un enlace estático. También intenté haciendo:

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) 

Y todavía recibo los mismos mensajes de error.

Respuesta

10

Para mingw32 puede añadir definición BOOST_THREAD_USE_LIB. Y la vinculación con boost :: thread funcionará. También es posible que necesite el paquete Threads (pero no estoy seguro, puede ser que solo necesite plataformas * nix).

Aquí está la parte de mi CMakeLists. Lo copié del proyecto, que utiliza impulsar :: hilo, y compila bajo MinGW gcc-(y otros compiladores):

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

Al agregar BOOST_THREAD_USE_LIB lo arregló para mí. Me he estado rascando la cabeza en esto durante la última hora más o menos. – CadentOrange

3

En mi opinión, esta pregunta es similar a this question y this one. Mi mejor opción sería que necesitas la misma resolución que en mi respuesta al first question.

recomendaría encarecidamente el uso de find_package (Boost) y tener cuidado con la 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

He intentado todo, desde esos puestos ya, sino también todo lo que ahora vuelto a inspeccionar y todo el tiempo Me sale el mismo error. Encuentra la biblioteca y establece las inclusiones a la perfección, pero de alguna manera simplemente no se vincula ... También recompuse el impulso usando '--build-type = complete' y traté de hacer un enlace dinámico, y aún así ganó 't work ... –

+0

En mi caso tuve que agregar 'find_package (Boost COMPONENTS thread system REQUIRED)' y 'target_link_libraries ( $ {Boost_LIBRARIES})' ¡por supuesto! – Tanasis

Cuestiones relacionadas