[英]ros packages with C++14 lambda support
Ok, since I'm working on a private project using ROS Framework, my motivation was raised by the new C++14 auto lambda feature (see this link for a better explanation). 好的,由于我正在使用ROS Framework进行私人项目开发,因此我的动力是由新的C ++ 14自动Lambda功能引起的(请参阅此链接以获取更好的说明)。
the problem I am facing is that ROS framework is sticking to the old days C++03 for it's compilation . 我面临的问题是ROS框架在编译时坚持使用旧的C ++ 03 。 So, how can I enable C++14 compilation support and how can I use the lambda functions ?
因此,如何启用C ++ 14编译支持以及如何使用lambda函数?
Here's a simple nodes for testing with the lambda function (in the subscriber node) : 这是一个使用lambda函数进行测试的简单节点(在订户节点中):
publisher : 发布者:
#include <ros/ros.h> #include <std_msgs/String.h> #include <std_msgs/Int8.h> #include <sstream> int main(int argc, char **argv) { ros::init(argc, argv, "talker"); ros::NodeHandle n; ros::Publisher string_pub = n.advertise<std_msgs::String>("chatter_string", 1000); ros::Publisher int_pub = n.advertise<std_msgs::Int8>("chatter_int", 1000); ros::Rate loop_rate(10); int count = 0; while (ros::ok()) { std_msgs::String str_msg; std::stringstream ss; ss << "hello world "; str_msg.data = ss.str(); std_msgs::Int8 int_msg; int_msg.data = count; string_pub.publish(str_msg); int_pub.publish(int_msg); ros::spinOnce(); loop_rate.sleep(); ++count; } return 0; }
subscriber : 订户:
#include <ros/ros.h> #include <std_msgs/String.h> #include <std_msgs/Int8.h> //generic lambda to add two variables auto func = [](auto input) { return input + input; }; void StringCallback(const std_msgs::String::ConstPtr& msg) { ROS_INFO("I heard: [%s]", func(msg->data).c_str()); } void IntCallback(const std_msgs::Int8::ConstPtr& msg) { int result = msg->data; ROS_INFO("I heard: [%d]", func(result)); } int main(int argc, char **argv) { ros::init(argc, argv, "listener"); ros::NodeHandle n; ros::Subscriber string_sub = n.subscribe("chatter_string", 1000, StringCallback); ros::Subscriber int_sub = n.subscribe("chatter_int", 1000, IntCallback); ros::spin(); return 0; }
CMakeLists : CMakeLists:
cmake_minimum_required(VERSION 2.8.3) project(tutocpp14) find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs ) catkin_package() include_directories( ${catkin_INCLUDE_DIRS} include ) add_executable(listener src/listener.cpp) add_executable(publisher src/publisher.cpp) target_link_libraries(listener ${catkin_LIBRARIES}) target_link_libraries(publisher ${catkin_LIBRARIES})
Making the package after this configuration throw me the following error : 在进行此配置后制作软件包会引发以下错误:
tutocpp14/src/listener.cpp:10:43: error: ‘func’ was not declared in this scope
ROS_INFO("I heard: [%s]", func(msg->data).c_str());
It turned out that I can add the C++14 to ROS for my package compilation. 事实证明,我可以将C ++ 14添加到ROS进行程序包编译。 Although this is not advised for publishing the packages.
尽管不建议您发布软件包。
Use of C++11/C++14 features and filesystem/networking/etc... TS's (Technical Specifications) is allowed if they are checked for at configure time and equivalent functionality can be provided with the extra compiler features.
使用C ++ 11 / C ++ 14功能和文件系统/网络/等...如果在配置时检查了TS(技术规格),则可以使用TS(技术规格),并且可以通过其他编译器功能提供同等功能。 Support for C++11 is now a compiler requirement, but the API of the packages included in desktop-full will not use any C++11-specific feature.
现在,对C ++ 11的支持是编译器的要求,但是,完全桌面版中包含的软件包的API将不使用任何特定于C ++ 11的功能。 External packages are encouraged to follow this guideline.
鼓励外部软件包遵循此指南。
But, still that I am not intending to publish my work for now, so, I can get myself the permit to do so. 但是,我仍然不打算现在发表我的作品,因此,我可以自己获得这样做的许可。
To get C++14 support for a ROS package, I have to set the -std=c++14 flag by adding the following lines in my CMakelists.txt
file : 为了获得对ROS包的C ++ 14支持,我必须通过在
CMakelists.txt
文件中添加以下几行来设置-std = c ++ 14标志:
# check c++14 / c++0x
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX14)
set(CMAKE_CXX_FLAGS "-std=c++14")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "-std=c++0x")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++14 support. Please use a different C++ compiler.")
endif()
this will check the existence of C++14 in the compiler and throw an error if not. 这将检查编译器中是否存在C ++ 14,如果没有,则抛出错误。 Now, it works like a charm.
现在,它就像一种魅力。
Hope that helps. 希望能有所帮助。 Cheers.
干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.