小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
请问ros2在使用make编译的时候报这个错误是怎么回事儿阿?
-
.cpp file
#include "rclcpp/rclcpp.hpp"int main(int argc, char** argv)
{
rclcpp::init(argc, argv);
auto node = std::make_sharedrclcpp::Node("test_node");
RCLCPP_INFO(node->get_logger(), "Hello, ROS2!");
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}cmakelists.txt
cmake_minimum_required(VERSION 3.20)
project(test_node)
add_executable(test_node test_node.cpp)find_package(rclcpp REQUIRED)
target_include_directories(test_node PUBLIC ${rclcpp_INCLUDE_DIRS})
target_link_libraries(test_node ${rclcpp_LIBRARIES})In file included from /opt/ros/humble/include/rclcpp/rclcpp/publisher.hpp:42:0,
from /opt/ros/humble/include/rclcpp/rclcpp/topic_statistics/subscription_topic_statistics.hpp:31,
from /opt/ros/humble/include/rclcpp/rclcpp/subscription.hpp:50,
from /opt/ros/humble/include/rclcpp/rclcpp/any_executable.hpp:25,
from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategy.hpp:25,
from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategies.hpp:18,
from /opt/ros/humble/include/rclcpp/rclcpp/executor_options.hpp:20,
from /opt/ros/humble/include/rclcpp/rclcpp/executor.hpp:37,
from /opt/ros/humble/include/rclcpp/rclcpp/executors/multi_threaded_executor.hpp:25,
from /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:21,
from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:155,
from /home/luyuan/桌面/all_codes/ros2_learn/test_node.cpp:1:
/opt/ros/humble/include/rclcpp/rclcpp/publisher_options.hpp:69:10: error: ‘is_void_v’ is not a member of ‘std’
std::is_void_v<typename std::allocator_traits<Allocator>::value_type>,
^~~~~~~~~
/opt/ros/humble/include/rclcpp/rclcpp/publisher_options.hpp:69:10: note: suggested alternative: ‘is_void’
std::is_void_v<typename std::allocator_traits<Allocator>::value_type>,
^~~~~~~~~
is_void
/opt/ros/humble/include/rclcpp/rclcpp/publisher_options.hpp:69:73: error: expected ‘(’ before ‘>’ token
std::is_void_v<typename std::allocator_traits<Allocator>::value_type>,
^
/opt/ros/humble/include/rclcpp/rclcpp/publisher_options.hpp:69:74: error: expected primary-expression before ‘,’ token
std::is_void_v<typename std::allocator_traits<Allocator>::value_type>,
^
In file included from /opt/ros/humble/include/rclcpp/rclcpp/any_executable.hpp:25:0,
from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategy.hpp:25,
from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategies.hpp:18,
from /opt/ros/humble/include/rclcpp/rclcpp/executor_options.hpp:20,
from /opt/ros/humble/include/rclcpp/rclcpp/executor.hpp:37,
from /opt/ros/humble/include/rclcpp/rclcpp/executors/multi_threaded_executor.hpp:25,
from /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:21,
from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:155,
from /home/luyuan/桌面/all_codes/ros2_learn/test_node.cpp:1:
/opt/ros/humble/include/rclcpp/rclcpp/subscription.hpp:295:10: error: ‘is_same_v’ is not a member of ‘std’
std::is_same_v<TakeT, SubscribedType>,
^~~~~~~~~
/opt/ros/humble/include/rclcpp/rclcpp/subscription.hpp:295:10: note: suggested alternative: ‘is_same’
std::is_same_v<TakeT, SubscribedType>,
^~~~~~~~~
is_same
/opt/ros/humble/include/rclcpp/rclcpp/subscription.hpp:293:8: error: parse error in template argument list
std::enable_if_t<
^~~~~~~~~~~~
!rosidl_generator_traits::is_message<TakeT>::value &&
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
std::is_same_v<TakeT, SubscribedType>,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/ros/humble/include/rclcpp/rclcpp/subscription.hpp:295:42: error: expected unqualified-id before ‘,’ token
std::is_same_v<TakeT, SubscribedType>,
^
In file included from /opt/ros/humble/include/rclcpp/rclcpp/subscription_base.hpp:32:0,
from /opt/ros/humble/include/rclcpp/rclcpp/callback_group.hpp:29,
from /opt/ros/humble/include/rclcpp/rclcpp/any_executable.hpp:20,
from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategy.hpp:25,
from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategies.hpp:18,
from /opt/ros/humble/include/rclcpp/rclcpp/executor_options.hpp:20,
from /opt/ros/humble/include/rclcpp/rclcpp/executor.hpp:37,
from /opt/ros/humble/include/rclcpp/rclcpp/executors/multi_threaded_executor.hpp:25,
from /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:21,
from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:155,
from /home/luyuan/桌面/all_codes/ros2_learn/test_node.cpp:1:
/opt/ros/humble/include/rclcpp/rclcpp/any_subscription_callback.hpp: In instantiation of ‘class rclcpp::AnySubscriptionCallback<rcl_interfaces::msg::ParameterEvent_<std::allocator<void> >, std::allocator<void> >’:
/opt/ros/humble/include/rclcpp/rclcpp/subscription.hpp:423:49: required from ‘class rclcpp::Subscription<rcl_interfaces::msg::ParameterEvent_<std::allocator<void> > >’
/opt/ros/humble/include/rclcpp/rclcpp/parameter_client.hpp:208:69: required from here
/opt/ros/humble/include/rclcpp/rclcpp/any_subscription_callback.hpp:977:3: error: no type named ‘variant_type’ in ‘struct rclcpp::detail::AnySubscriptionCallbackHelper<rcl_interfaces::msg::ParameterEvent_<std::allocator<void> >, std::allocator<void>, false>’
get_variant()
^~~~~~~~~~~
/opt/ros/humble/include/rclcpp/rclcpp/any_subscription_callback.hpp:983:3: error: no type named ‘variant_type’ in ‘struct rclcpp::detail::AnySubscriptionCallbackHelper<rcl_interfaces::msg::ParameterEvent_<std::allocator<void> >, std::allocator<void>, false>’
get_variant() const
^~~~~~~~~~~
/opt/ros/humble/include/rclcpp/rclcpp/any_subscription_callback.hpp:993:34: error: no type named ‘variant_type’ in ‘struct rclcpp::detail::AnySubscriptionCallbackHelper<rcl_interfaces::msg::ParameterEvent_<std::allocator<void> >, std::allocator<void>, false>’
typename HelperT::variant_type callback_variant_;
^~~~~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/test_node.dir/build.make:76:CMakeFiles/test_node.dir/test_node.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:85:CMakeFiles/test_node.dir/all] 错误 2
make: *** [Makefile:101:all] 错误 2 -
从错误信息来看,你遇到的问题主要与C++标准库中的类型特性(type traits)相关。错误提示表明
std::is_void_v
和std::is_same_v
不是std
的成员,这通常意味着编译器没有识别这些类型特性,这可能是因为你的编译器或编译选项不支持C++17或更高版本,而std::is_void_v
和std::is_same_v
是C++17中引入的。ROS 2 Humble 默认要求使用C++17或更高版本的编译器。以下是一些可能的解决步骤:
-
检查编译器版本:
确保你使用的编译器支持C++17。对于GCC,版本应该至少是8.2;对于Clang,版本应该至少是7.0。你可以通过运行gcc --version
或clang --version
来检查你的编译器版本。 -
设置CMake使用C++17:
在你的CMakeLists.txt
文件中,确保设置了正确的C++标准。虽然ROS 2的CMake模板通常已经设置了这一点,但你可以显式地添加或确认以下行:set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON)
-
清理并重新构建:
在修改CMakeLists.txt或编译器设置后,确保清理你的构建目录(如果有的话)并从头开始构建。这可以通过删除build
或CMakeCache.txt
文件以及使用make clean
(如果可用)来完成,然后再次运行colcon build
或cmake .. && make
(取决于你使用的是colcon
还是传统的make
)。 -
检查环境变量:
确保你的环境变量正确设置,特别是与ROS 2和编译器相关的变量。这包括CMAKE_PREFIX_PATH
,它应该包含ROS 2的安装路径。 -
使用
colcon
代替make
:
ROS 2推荐使用colcon
作为构建系统。如果你的项目是一个标准的ROS 2包,使用colcon build
代替make
可能更容易解决依赖和构建问题。确保你的工作空间已经正确设置,并且你已经初始化了它(使用colcon build --packages-up-to <package-name>
)。 -
查看依赖关系:
确保所有必要的依赖项都已正确安装,并且CMakeLists.txt文件中的find_package
调用正确无误。
如果以上步骤仍然无法解决问题,请检查ROS 2的官方文档或社区论坛,看看是否有其他人遇到并解决了类似的问题。
以上为生成回答,仅供参考~
-
-
@luyuan 看最新版教程