问题描述:

I will try to explain in detail the difficulties I encountered.

Recently I want to use boost libraries in Ubuntu Mint 18 x64. So I download the newest version of it which is 1.62.0. Sha256sum is fine. Then I started compiled it with "./bootstrap.sh" and "./b2". Finally copied then to "/usr/local" with "sudo ./b2 install". So far so good.

I am using Clion right now, so new job is to add libraries in Cmakelist.txt. Code below is what I have added.

set(BOOST_ROOT /usr/local)

set(Boost_USE_STATIC_LIBS ON)

set(Boost_USE_MULTITHREADED ON)

set(Boost_USE_STATIC_RUNTIME OFF)

find_package(Boost 1.62.0)

if(Boost_FOUND)

include_directories(${Boost_INCLUDE_DIRS})

add_executable(BoostTest main.cpp)

target_link_libraries(BoostTest ${Boost_LIBRARIES})

endif()

First thing I want to explain is that I know it is not good to set up a specific location in CMake, but without it Cmake cannot find it. So I just do that. And, at this moment, CMake did not post any error info. Here is my hello_world demo.

#include <iostream>

#include <boost/thread/testable_mutex.hpp>

int main() {

std::cout << "Hello, World!" << std::endl;

return 0;

}

I just include a .hpp for test, nothing else. When I attemped to run it. I got these.

-- Boost version: 1.62.0

-- Configuring done

-- Generating done

-- Build files have been written to: /home/vita-nove/ClionProjects/BoostTest/cmake-build-debug

[ 50%] Building CXX object CMakeFiles/BoostTest.dir/main.cpp.o

[100%] Linking CXX executable BoostTest

CMakeFiles/BoostTest.dir/main.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':

/usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'

/usr/local/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'

/usr/local/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'

collect2: error: ld returned 1 exit status

CMakeFiles/BoostTest.dir/build.make:94: recipe for target 'BoostTest' failed

make[3]: *** [BoostTest] Error 1

CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/BoostTest.dir/all' failed

make[2]: *** [CMakeFiles/BoostTest.dir/all] Error 2

CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/BoostTest.dir/rule' failed

make[1]: *** [CMakeFiles/BoostTest.dir/rule] Error 2

Makefile:118: recipe for target 'BoostTest' failed

make: *** [BoostTest] Error 2

Wired thing is that I do find some .hpp file in External Libraries. They are all located in /include/boost. In /usr/local/lib and /usr/local/include/boost everything shows up as I expect.

Sorry for my bad English. After spending nearly whole day searching and testing I cannot solve it. I don't know what dependencies I have missed or something else. Anyway, Thank you for reading this, hope someone will help.

网友答案:

You have to tell cmake that you need the thread library

set(BOOST_ROOT /usr/local)

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost 1.62.0 COMPONENTS thread system) # <-- here
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(BoostTest main.cpp)
    target_link_libraries(BoostTest ${Boost_LIBRARIES})
endif()
相关阅读:
Top