【发布时间】:2021-04-28 12:58:45
【问题描述】:
我正在尝试在 ros 节点中使用 pthread 库,我将其包含为 #include <pthread>。当我运行 catkin_make 时,我收到以下错误。我创建了一个简单的线程std::thread m1(move, 1);
thread: No such file or directory
#include <pthread>
^~~~~~~~~
移动函数的签名是void move(short axis_no, short direction = 0),我将线程实例化为
std::thread m1(move, 1);
m1.join();
我尝试在我的 CmakeLists.txt 中添加 pthread 库,如下所示
add_compile_options(-std=c++11 -pthread) 和
target_link_libraries(pthread)。有没有办法可以在 ros 节点中使用 pthread 库?
谢谢。
【问题讨论】:
-
pthread.h也许?但你可能想要<thread> -
没有
pthread标头;有pthread.h。其次,如果您使用的是兼容的 C++11 工具链,那么为什么要直接使用 pthreads 是一个谜,因为<thread>提供了丰富的功能集,可以让您摆脱pthread.h的疯狂。 -
在现代 C++(C++11 或更高版本)程序中直接使用 pthread 吗?
-
#include <thread>那么您的代码std::thread m1(move, 1);看起来不错。如果你使用 CMake,那么set(CMAKE_CXX_STANDARD 11);如果你想告诉 gcc 使用 pthread 库,那么它是-lpthread -
我包含了
thread,我收到了错误/usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (*)(short int, short int)> >::_M_invoke(std::thread::_Invoker<std::tuple<void (*)(short int, short int)> >::_Indices)’ operator()()