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

时间:2021-12-07 21:01:13

I am using Mac OS 10.11, and clang as my C/C++ compiler. I installed boost with the easiest way brew install boost

我正在使用Mac OS 10.11和clang作为我的C/ c++编译器。我安装了boost与最简单的方式brew安装boost

First I tried these code

首先我试过这些代码

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

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

It could work fine. So it seems that boost is set up correctly.

它可以正常工作。因此,boost的设置似乎是正确的。

But when to try to use #include<boost/thread.hpp> and the example code as shown below:

但是什么时候尝试使用#include 和示例代码如下所示:

#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();
}

Then I got these

然后我得到了这些

/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

I google this problem for a while. I tried to set library dir set(Boost_LIBRARY_DIRS /usr/local/lib) link_directories(${Boost_LIBRARY_DIRS}).

这个问题我已经解决了一段时间了。我尝试设置库dir设置(Boost_LIBRARY_DIRS /usr/local/lib) link_directory (${Boost_LIBRARY_DIRS})。

My CMakeLists.txt is here:

我的CMakeLists。三是在这里:

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

I also tried clang++ main.cpp -lboost_thread-mt. And then got these:

我还尝试了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)

Can anyone give me some help? Thank you very much!

谁能帮我个忙吗?非常感谢!


UPDATE: with the find_package command as @Angew said, I got some new error.

更新:使用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

UPDATE

更新

I tried some elder version of boost but sill the same. And if I use target_include_directories(untitled PRIVATE ${Boost_INCLUDE_DIRS}) and target_link_libraries(untitled ${Boost_LIBRARIES})

我尝试了一些老版本的boost,但仍然是一样的。如果我使用target_include_directory(尚未命名的私有${Boost_INCLUDE_DIRS})和target_link_libraries(尚未命名的${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

UPDATE

更新

@Angew found the true answer.

@Angew找到了真正的答案。

I've updated the answer again. It seems Boost.Thread requires you to use Boost.System too. This would be handled by the imported targets, but since you can't use those, you apparently have to explicitly request it.

我已经更新了答案。似乎提振。线程需要您使用Boost。系统。这将由导入的目标来处理,但是由于不能使用这些目标,显然必须显式地请求它。

find_package(Boost REQUIRED COMPONENTS thread system) makes it work fine.

find_package(增强必需的组件线程系统)使它工作得很好。

1 个解决方案

#1


1  

The correct CMake way to interface with third-party packages is to use a find_package() call, which will use either a find module (shipped with CMake), or a package config file (shipped by the third-party package), to locate the necessary libraries, directories etc.

与第三方包进行接口的正确CMake方法是使用find_package()调用,该调用将使用find模块(随CMake一起提供)或包配置文件(由第三方包提供)来定位所需的库、目录等。

In the case of Boost, there is a find module shipped with CMake itself. It offers a very tunable interface, you can read its documentation for details. However, here's the most basic way you could use it:

在Boost的情况下,有一个与CMake本身一起发送的find模块。它提供了一个非常可调的接口,您可以阅读它的文档了解详细信息。然而,以下是最基本的使用方法:

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)

Based on this message you're receiving:

根据你收到的信息:

Imported targets not available for Boost version 106300

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

it seems the imported-target approach cannot be used with your version of Boost. You will therefore have to fall back to variables provided by the find module:

似乎插入目标方法不能用于您的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})

#1


1  

The correct CMake way to interface with third-party packages is to use a find_package() call, which will use either a find module (shipped with CMake), or a package config file (shipped by the third-party package), to locate the necessary libraries, directories etc.

与第三方包进行接口的正确CMake方法是使用find_package()调用,该调用将使用find模块(随CMake一起提供)或包配置文件(由第三方包提供)来定位所需的库、目录等。

In the case of Boost, there is a find module shipped with CMake itself. It offers a very tunable interface, you can read its documentation for details. However, here's the most basic way you could use it:

在Boost的情况下,有一个与CMake本身一起发送的find模块。它提供了一个非常可调的接口,您可以阅读它的文档了解详细信息。然而,以下是最基本的使用方法:

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)

Based on this message you're receiving:

根据你收到的信息:

Imported targets not available for Boost version 106300

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

it seems the imported-target approach cannot be used with your version of Boost. You will therefore have to fall back to variables provided by the find module:

似乎插入目标方法不能用于您的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})