支持 C++14 lambda 的 ros 包
ros packages with C++14 lambda support
好的,因为我正在使用 ROS 框架开发一个私人项目,所以新的 C++14 auto lambda 功能激发了我的动力(请参阅此 link 以获得更好的解释)。
我面临的问题是 ROS 框架还在沿用旧时代 C++03 for it's compilation。那么,如何启用 C++14 编译支持以及如何使用 lambda 函数?
这是一个使用 lambda 函数进行测试的简单节点(在订阅者节点中):
发布者:
#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;
}
订阅者:
#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 :
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})
在此配置后制作包抛出以下错误:
tutocpp14/src/listener.cpp:10:43: error: ‘func’ was not declared in this scope
ROS_INFO("I heard: [%s]", func(msg->data).c_str());
原来我可以将C++14添加到ROS中进行我的包编译。虽然不建议发布包。
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. 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. External packages are encouraged to follow this guideline.
但是,我还是暂时不打算发表我的作品,所以,我可以让自己获得这样做的许可。
为了获得对 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()
这将检查编译器中是否存在 C++14,如果不存在则抛出错误。现在,它就像一个魅力。
希望对您有所帮助。干杯。
好的,因为我正在使用 ROS 框架开发一个私人项目,所以新的 C++14 auto lambda 功能激发了我的动力(请参阅此 link 以获得更好的解释)。
我面临的问题是 ROS 框架还在沿用旧时代 C++03 for it's compilation。那么,如何启用 C++14 编译支持以及如何使用 lambda 函数?
这是一个使用 lambda 函数进行测试的简单节点(在订阅者节点中):
发布者:
#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; }
订阅者:
#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 :
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})
在此配置后制作包抛出以下错误:
tutocpp14/src/listener.cpp:10:43: error: ‘func’ was not declared in this scope
ROS_INFO("I heard: [%s]", func(msg->data).c_str());
原来我可以将C++14添加到ROS中进行我的包编译。虽然不建议发布包。
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. 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. External packages are encouraged to follow this guideline.
但是,我还是暂时不打算发表我的作品,所以,我可以让自己获得这样做的许可。
为了获得对 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()
这将检查编译器中是否存在 C++14,如果不存在则抛出错误。现在,它就像一个魅力。
希望对您有所帮助。干杯。