Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake and Boost

I've searched and found out that a lot of people have the same problem, but no solution exists.

I'm using CMake to generate Makefiles for MinGW and when compiling I'm getting an 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'

This seems to be a linking problem, I get it. My CMake configuration is:

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)

First I tried using find_package(Boost COMPONENTS thread) and it was working the same way, so I thought to try to do this manually and I still get the same error.

Any insights on this?

I've compiled it for mingw using bjam and as a static link. Also tried doing:

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)

And I still get the same error messages.

like image 253
Nemanja Miljković Avatar asked Feb 13 '12 02:02

Nemanja Miljković


People also ask

Can I build Boost with CMake?

One of my current projects relies on Boost. Python, which requires a more recent version of Boost (1.64) than the one (1.54) provided by my Linux distribution (Ubuntu 14.04).

How do you add a Boost to Cmakelist?

For CMake, you need to add boost_program_options to the list of libraries, and IIRC this is done via SET(liblist boost_program_options) in your CMakeLists. txt .

What is Boost C++ used for?

Boost is a set of libraries for the C++ programming language that provides support for tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions, and unit testing. It contains 164 individual libraries (as of version 1.76).


2 Answers

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}
    )
like image 141
Alexey Avatar answered Sep 30 '22 06:09

Alexey


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} )
like image 32
André Avatar answered Sep 30 '22 07:09

André