为什么我不能链接到boost的线程库?

时间:2017-04-12 06:27:39

标签: c++ multithreading boost cmake

我使用的是Mac OS 10.11,并且作为我的C / C ++编译器。 我以最简单的方式brew install boost

安装了boost

首先我尝试了这些代码

#include <iostream>
#include <boost/array.hpp>

int main() {
    boost::array<int, 4> arr = {1, 2, 3, 4};
    std::cout << arr[3];
    return 0;
}

它可以正常工作。所以似乎正确设置了升压。

但是何时尝试使用#include<boost/thread.hpp>和示例代码如下所示:

#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>

void wait(int seconds)
{
    boost::this_thread::sleep_for(boost::chrono::seconds{seconds});
}

void thread()
{
    for (int i = 0; i < 5; ++i)
    {
        wait(1);
        std::cout << i << '\n';
    }
}

int main()
{
    boost::thread t{thread};
    t.join();
}

然后我得到了这些

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/vita-nove/CLionProjects/untitled/cmake-build-debug --target untitled -- -j 4
[ 50%] Linking CXX executable untitled
Undefined symbols for architecture x86_64:
  "boost::this_thread::hidden::sleep_for(timespec const&)", referenced from:
  boost::this_thread::sleep_for(boost::chrono::duration<long long,    boost::ratio<1l, 1000000000l> > const&) in main.cpp.o
  "boost::detail::thread_data_base::~thread_data_base()", referenced from:
  boost::detail::thread_data<void (*)()>::~thread_data() in main.cpp.o
  "boost::system::system_category()", referenced from:
  boost::thread_exception::thread_exception(int, char const*) in main.cpp.o
  ___cxx_global_var_init.2 in main.cpp.o
  "boost::system::generic_category()", referenced from:
  ___cxx_global_var_init in main.cpp.o
  ___cxx_global_var_init.1 in main.cpp.o
  "boost::thread::join_noexcept()", referenced from:
  boost::thread::join() in main.cpp.o
  "boost::thread::native_handle()", referenced from:
  boost::thread::get_id() const in main.cpp.o
  "boost::thread::start_thread_noexcept()", referenced from:
  boost::thread::start_thread() in main.cpp.o
  "boost::thread::detach()", referenced from:
  boost::thread::~thread() in main.cpp.o
  "typeinfo for boost::detail::thread_data_base", referenced from:
  typeinfo for boost::detail::thread_data<void (*)()> in main.cpp.o
  "vtable for boost::detail::thread_data_base", referenced from:
  boost::detail::thread_data_base::thread_data_base() in main.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2

我谷歌这个问题已经有一段时间了。我尝试设置库目录set(Boost_LIBRARY_DIRS /usr/local/lib) link_directories(${Boost_LIBRARY_DIRS})

我的CMakeLists.txt在这里:

cmake_minimum_required(VERSION 3.7)
project(untitled)

set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES main.cpp)
add_executable(untitled main.cpp)
set(Boost_LIBRARY_DIRS /usr/local/lib)
link_directories(${Boost_LIBRARY_DIRS})

我也试过clang++ main.cpp -lboost_thread-mt。 然后得到了这些:

vita-nove@MacBook-Pro:~/CLionProjects/untitled$ clang++ main.cpp -lboost_thread-mt
Undefined symbols for architecture x86_64:
  "boost::system::system_category()", referenced from:
  boost::thread_exception::thread_exception(int, char const*) in main-964777.o
  ___cxx_global_var_init.2 in main-964777.o
  "boost::system::generic_category()", referenced from:
  ___cxx_global_var_init in main-964777.o
  ___cxx_global_var_init.1 in main-964777.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

有人能给我一些帮助吗?非常感谢你!

更新: 使用find_package命令@Angew说,我收到了一些新错误。

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/vita-nove/CLionProjects/untitled/cmake-build-debug --target untitled -- -j 4
CMake Warning at /Applications/CLion.app/Contents/bin/cmake/share/cmake-3.7/Modules/FindBoost.cmake:744 (message):
  Imported targets not available for Boost version 106300
Call Stack (most recent call first):
  /Applications/CLion.app/Contents/bin/cmake/share/cmake-3.7/Modules/FindBoost.cmake:848 (_Boost_COMPONENT_DEPENDENCIES)
  /Applications/CLion.app/Contents/bin/cmake/share/cmake-3.7/Modules/FindBoost.cmake:1435 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:9 (find_package)


-- Boost version: 1.63.0
-- Found the following Boost libraries:
--   thread
-- Configuring done
CMake Error at CMakeLists.txt:7 (add_executable):
  Target "untitled" links to target "Boost::thread" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?


-- Generating done
-- Build files have been written to: /Users/vita-nove/CLionProjects/untitled/cmake-build-debug
make: *** [cmake_check_build_system] Error 1

更新

我尝试了一些老版本的增强版但是同样的。 如果我使用target_include_directories(untitled PRIVATE ${Boost_INCLUDE_DIRS})target_link_libraries(untitled ${Boost_LIBRARIES})

Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
Undefined symbols for architecture x86_64:
  "boost::system::system_category()", referenced from:
  boost::thread_exception::thread_exception(int, char const*) in main.cpp.o
  ___cxx_global_var_init.2 in main.cpp.o
  "boost::system::generic_category()", referenced from:
  ___cxx_global_var_init in main.cpp.o
  ___cxx_global_var_init.1 in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [untitled] Error 1
make[1]: *** [CMakeFiles/untitled.dir/all] Error 2
make: *** [all] Error 2

更新

@Angew找到了真正的答案。

  

我再次更新了答案。似乎Boost.Thread也要求你使用Boost.System。这将由导入的目标处理,但由于您无法使用它们,您显然必须明确请求它。

find_package(Boost REQUIRED COMPONENTS thread system)让它工作正常。

1 个答案:

答案 0 :(得分:1)

与第三方软件包交互的正确CMake方法是使用find_package()调用,该调用将使用查找模块(随CMake一起提供)或软件包配置文件(由第三方提供)包),找到必要的库,目录等。

在Boost的情况下,CMake本身附带了一个find模块。它提供了一个非常可调的界面,您可以阅读其documentation了解详细信息。但是,这是您使用它的最基本方式:

cmake_minimum_required(VERSION 3.7)
project(untitled)

set(CMAKE_CXX_STANDARD 14)

find_package(Boost REQUIRED COMPONENTS thread)
add_executable(untitled main.cpp)
target_link_libraries(untitled Boost::thread)

根据此消息,您将收到:

  

导入的目标不适用于Boost版本106300

似乎导入目标方法不能与您的Boost版本一起使用。因此,您必须回退到find模块提供的变量:

cmake_minimum_required(VERSION 3.7)
project(untitled)

set(CMAKE_CXX_STANDARD 14)

find_package(Boost REQUIRED COMPONENTS thread system)

add_executable(untitled main.cpp)
target_include_directories(untitled PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(untitled ${Boost_LIBRARIES})