ros2节点编译失败

ROS2节点编译失败,更改C++标准解决

刚开始使用ros2,编译一个节点,怎么改都不能通过。报错如下
ATMK-176 12:25:47
/usr/include/c++/9/bits/stl_algo.h:120:14: required from ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const std::weak_ptrrclcpp::GuardCondition, std::vector<std::weak_ptrrclcpp::GuardCondition > >; _Predicate = __gnu_cxx::__ops::_Iter_pred<rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::GuardCondition; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::GuardCondition >]::<lambda(const int&)> >]’
/usr/include/c++/9/bits/stl_algo.h:161:23: required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<const std::weak_ptrrclcpp::GuardCondition
, std::vector<std::weak_ptrrclcpp::GuardCondition > >; _Predicate = __gnu_cxx::__ops::_Iter_pred<rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::GuardCondition; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::GuardCondition >]::<lambda(const int&)> >]’
/usr/include/c++/9/bits/stl_algo.h:3969:28: required from ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<const std::weak_ptrrclcpp::GuardCondition, std::vector<std::weak_ptrrclcpp::GuardCondition > >; _Predicate = rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::GuardCondition; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::GuardCondition >]::<lambda(const int&)>]’
/usr/include/c++/9/bits/stl_algo.h:526:47: required from ‘bool std::none_of(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<const std::weak_ptrrclcpp::GuardCondition
, std::vector<std::weak_ptrrclcpp::GuardCondition > >; _Predicate = rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::GuardCondition; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::GuardCondition >]::<lambda(const int&)>]’
/usr/include/c++/9/bits/stl_algo.h:544:27: required from ‘bool std::any_of(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<const std::weak_ptrrclcpp::GuardCondition, std::vector<std::weak_ptrrclcpp::GuardCondition > >; _Predicate = rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::GuardCondition; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::GuardCondition >]::<lambda(const int&)>]’
/opt/ros/foxy/include/rclcpp/wait_set_policies/dynamic_storage.hpp:223:23: required from ‘static bool rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::GuardCondition; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::GuardCondition >]’
/opt/ros/foxy/include/rclcpp/wait_set_policies/dynamic_storage.hpp:265:69: required from here
/usr/include/c++/9/bits/predefined_ops.h:283:11: error: no match for call to ‘(rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::GuardCondition; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::GuardCondition >]::<lambda(const int&)>) (const std::weak_ptrrclcpp::GuardCondition&)’
283 | { return bool(_M_pred(
__it)); }
| ^~~~~~~~~~~~~~~~~~~~
In file included from /opt/ros/foxy/include/rclcpp/wait_set.hpp:25,
from /opt/ros/foxy/include/rclcpp/rclcpp.hpp:157,
from /home/xysk/ros2_ws/src/face_recognition/src/feature_match.cpp:1:
/opt/ros/foxy/include/rclcpp/wait_set_policies/dynamic_storage.hpp:226:7: note: candidate: ‘rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::GuardCondition; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::GuardCondition >]::<lambda(const int&)>’
226 | [&entity](const auto & inner) {return &entity == inner.lock().get();});
| ^
/opt/ros/foxy/include/rclcpp/wait_set_policies/dynamic_storage.hpp:226:7: note: no known conversion for argument 1 from ‘const std::weak_ptrrclcpp::GuardCondition’ to ‘const int&’
In file included from /usr/include/c++/9/bits/stl_algobase.h:71,
from /usr/include/c++/9/memory:62,
from /opt/ros/foxy/include/rclcpp/rclcpp.hpp:144,
from /home/xysk/ros2_ws/src/face_recognition/src/feature_match.cpp:1:
/usr/include/c++/9/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<const std::weak_ptrrclcpp::TimerBase, std::vector<std::weak_ptrrclcpp::TimerBase > >; _Predicate = rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::TimerBase; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::TimerBase >]::<lambda(const int&)>]’:
/usr/include/c++/9/bits/stl_algo.h:120:14: required from ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const std::weak_ptrrclcpp::TimerBase
, std::vector<std::weak_ptrrclcpp::TimerBase > >; _Predicate = __gnu_cxx::__ops::_Iter_pred<rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::TimerBase; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::TimerBase >]::<lambda(const int&)> >]’
/usr/include/c++/9/bits/stl_algo.h:161:23: required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<const std::weak_ptrrclcpp::TimerBase, std::vector<std::weak_ptrrclcpp::TimerBase > >; _Predicate = __gnu_cxx::__ops::_Iter_pred<rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::TimerBase; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::TimerBase >]::<lambda(const int&)> >]’
/usr/include/c++/9/bits/stl_algo.h:3969:28: required from ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<const std::weak_ptrrclcpp::TimerBase
, std::vector<std::weak_ptrrclcpp::TimerBase > >; _Predicate = rclcpp::wait_set_policies::DynamicStorage::storage_has_entity(const EntityT&, const SequenceOfEntitiesT&) [with EntityT = rclcpp::TimerBase; SequenceOfEntitiesT = std::vector<std::weak_ptrrclcpp::TimerBase >]::<lambda(const int&)>]’
/usr/include/c++/9/bits/stl_algo.h:526:47: required from ‘bool std::none_of(_IIter, _IIter, _Predicate) [with _IIter = __gn

后来发现,把CMAKE_CXX_STANDARD从11改为14就可以了。
在ROS 2中,cmake_cxx_standard 通常设置为 C++14 或 C++17,具体取决于ROS 2的版本和你的项目需求。
ROS 2 Foxy Fitzroy 及之后的版本默认使用 C++17。
ROS 2 Eloquent Elusor 及之前的版本默认使用 C++14。

<think>好的,用户想了解如何使用make来编译ROS2节点,需要具体的步骤或教程。首先,我需要回忆一下ROS2编译系统。ROS2主要使用colcon作为构建工具,而是传统的make。过,用户明确提到要用make,可能他们有一些遗留项目或者特定的需求。 首先,我需要确认ROS2是否支持直接使用make。通常,ROS2的包是使用ament_cmake或ament_python作为构建类型。对于C++项目,ament_cmake是基于CMake的,而CMake底层确实会生成Makefile。因此,用户可能指的是如何通过CMake生成Makefile,然后使用make命令进行编译。 接下来,我需要整理步骤。首先创建功能包,用户之前的引用中提到了ros2 pkg create命令,需要指定--build-type为ament_cmake,并添加依赖项如rclcpp。然后编写节点代码,比如一个简单的发布者或订阅者。之后需要编辑CMakeLists.txt文件,添加可执行文件和安装指令。编译步骤通常是进入工作空间,运行colcon build,但用户想用make,可能需要直接进入包目录,运行cmake .. && make。过,这样可能会处理依赖关系,所以需要提醒用户正确设置工作空间的环境变量。 同时,用户的引用中有多个创建包的示例,比如引用1和引用4提到了创建功能包的命令,需要注意目录结构,避免编译错误。需要指导用户正确配置CMakeLists.txt,包括find_package和ament_target_dependencies,以及安装目标的设置。 可能的问题点:用户可能混淆了colcon和make的关系。需要解释清楚colcon是ROS2的构建工具,它底层调用CMake生成Makefile,然后调用make。如果用户坚持仅用make,可能需要手动配置,但这可能复杂且容易出错,建议使用colcon。但按照用户要求,还是需要给出使用make的方法,同时提醒潜在的问题。 需要生成相关问题,比如如何配置CMakeLists.txt,如何处理依赖,或比较colcon和make的区别。确保回答结构清晰,分步骤说明,并引用用户提供的参考资料中的命令示例。</think>### 使用`make`编译ROS2节点的步骤说明 虽然ROS2推荐使用`colcon`作为标准构建工具,但通过`ament_cmake`生成的CMake项目仍支持传统的`make`编译方式。以下是具体步骤: --- #### 1. **创建ROS2功能包** 在ROS2工作空间(如`~/ros/src`)中创建功能包,并指定依赖项(如`rclcpp`): ```bash cd ~/ros/src ros2 pkg create --build-type ament_cmake my_make_node --dependencies rclcpp ``` 这一步会生成包目录`my_make_node`和默认的`CMakeLists.txt`文件[^2][^4]。 --- #### 2. **编写节点代码** 在`src`目录下创建C++节点文件(如`my_node.cpp`): ```cpp #include "rclcpp/rclcpp.hpp" class MyNode : public rclcpp::Node { public: MyNode() : Node("my_node") { RCLCPP_INFO(this->get_logger(), "Hello from make-compiled node!"); } }; int main(int argc, char** argv) { rclcpp::init(argc, argv); auto node = std::make_shared<MyNode>(); rclcpp::spin(node); rclcpp::shutdown(); return 0; } ``` --- #### 3. **修改`CMakeLists.txt`** 在`CMakeLists.txt`中添加可执行目标和安装规则: ```cmake # 添加可执行文件 add_executable(my_node src/my_node.cpp) # 链接ROS2依赖 ament_target_dependencies(my_node rclcpp) # 安装到lib目录 install(TARGETS my_node DESTINATION lib/${PROJECT_NAME}) ``` 此配置确保节点能被ROS2环境发现[^1][^3]。 --- #### 4. **使用`make`编译** 进入功能包目录,手动调用CMake生成Makefile后编译: ```bash cd ~/ros/src/my_make_node mkdir build && cd build cmake .. # 生成Makefile make # 编译节点 ``` 编译成功后,可执行文件位于`build`目录,但需通过`install`步骤部署到ROS2环境。 --- #### 5. **安装并运行节点** 通过`make install`将节点安装到工作空间的`install`目录: ```bash make install source ~/ros/install/setup.bash # 激活环境 ros2 run my_make_node my_node # 运行节点 ``` --- ### 关键注意事项 - **依赖管理**:`ament_target_dependencies`需明确声明依赖项(如`rclcpp`),否则编译失败- **环境隔离**:ROS2节点需通过`source setup.bash`加载环境变量,否则可能无法找到依赖库[^1][^3]。 - **推荐工具**:建议优先使用`colcon build`,因其自动处理依赖和跨包编译[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值