支持 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 函数进行测试的简单节点(在订阅者节点中):

在此配置后制作包抛出以下错误:

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,如果不存在则抛出错误。现在,它就像一个魅力。

希望对您有所帮助。干杯。