【问题标题】:Why can't I link to boost's thread libraries?为什么我不能链接到 boost 的线程库?
【发布时间】:2023-03-03 01:21:02
【问题描述】:

我使用的是 Mac OS 10.11,并将 clang 作为我的 C/C++ 编译器。 我用最简单的方式安装了 boost brew install boost

首先我尝试了这些代码

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

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

它可以正常工作。所以看来 boost 的设置是正确的。

但是什么时候尝试使用#include&lt;boost/thread.hpp&gt;,示例代码如下:

#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)

谁能帮帮我?非常感谢!


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

/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

更新

我尝试了一些旧版本的 boost,但还是一样。 如果我使用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) 让它工作正常。

【问题讨论】:

  • 看来您正在使用 CMake。你能提供你CMakeLists.txt的相关部分吗? (如何找到 Boost 以及如何设置可执行文件以使用它)。
  • @Angew 是的,当然,更新了。

标签: c++ multithreading boost cmake


【解决方案1】:

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

对于 Boost,CMake 本身附带了一个查找模块。它提供了一个非常可调的接口,您可以阅读它的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})

【讨论】:

  • 谢谢!我收到了CMake Error at CMakeLists.txt:7 (add_executable): Target "untitled" links to target "Boost::thread" but the target was not found. 之类的错误,也许我应该告诉 CMake boost 在哪里?
  • @AmorFati 你真的有合适的 Boost.Thread 版本吗?顺便说一句,如果它没有在 find_package 行上出现类似“找不到 Boost”的错误,那么 Boost 就被成功找到了(注意使用了 REQUIRED)。
  • @AmorFati 已编辑
  • 谢谢,这次更远了。顺便说一句,我在这里有一个问题。如您所见,boost/array.hpp 可以成功包含和链接。那么导致所有这些问题发生的 array.hpp 和 thread.hpp 之间的区别是什么?
  • @AmorFati 大多数 Boost 库都是标头,这意味着它们根本没有链接。 Boost.Array 也是如此。只有少数 Boost 库已编译部分,因此需要链接。 Boost.Thread 就是其中之一。详情请参考Boost docs
猜你喜欢
  • 1970-01-01
  • 2017-05-14
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
相关资源
最近更新 更多