ROS2 配置 linter 的代码格式化工具为 clang-format

ROS2 提供的测试框架提供代码静态检查功能(linter),在使用 ros2 pkg create 命令(完整命令如下,供参考)创建空白项目时,会默认启用代码静态检查。

bash 复制代码
ros2 pkg create --build-type ament_cmake hello_world --dependencies rclcpp rclpy

生成的 package.xml 存在以下代码行。

xml 复制代码
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

生成的 CMakeLists.txt 存在以下代码行。

cmake 复制代码
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()

然而,以上默认配置在检查代码格式化时,使用的工具是 uncrustify,这导致了我们的 .clang-format 文件无法被用于测试,colcon test 会因为代码格式不匹配而产生意外的失败用例。

如果希望使用 clang-format 而非 uncrustify,需要进行以下操作。

  1. 安装 ros-${ROS_DISTRO}-ament-cmake-clang-format

    bash 复制代码
    sudo apt install ros-${ROS_DISTRO}-ament-cmake-clang-format

    需要注意的是,还有一个包叫作 ros-${ROS_DISTRO}-ament-clang-format,它被以上包依赖,所以无需手动安装。

    如果因为某些原因你的环境变量没有 ROS_DISTRO,请将以上命令中的 ${ROS_DISTRO} 替换为你的 ROS 发行版,以 humble 为例,完整命令如下。

    bash 复制代码
    sudo apt install ros-humble-ament-cmake-clang-format
  2. CMakeLists.txt 中禁用 uncrustify。

    仿照已有的样板代码,增加下面的代码。

    cmake 复制代码
    set(ament_cmake_uncrustify_FOUND TRUE)
  3. package.xml 中增加 ament-clang-format 依赖。

    xml 复制代码
    <test_depend>ament_clang_format</test_depend>
  4. CMakeLists.txt 中配置要使用的 .clang-format 文件。参见 docs.ros.org/en/ros2_pac...

    cmake 复制代码
    set(ament_cmake_clang_format_CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/.clang-format")

    以上语句需要在 ament_lint_auto_find_test_dependencies() 之前设置。

CMakeLists.txt 修改后的内容如下,供参考。

cmake 复制代码
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
set(ament_cmake_uncrustify_FOUND TRUE)
set(ament_cmake_clang_format_CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/.clang-format")
ament_lint_auto_find_test_dependencies()
相关推荐
点云SLAM9 小时前
C++内存泄漏检测之Windows 专用工具(CRT Debug、Dr.Memory)和Linux 专业工具(ASan 、heaptrack)
linux·c++·windows·asan·dr.memory·c++内存泄漏检测·c++内存管理
浅念-9 小时前
C语言小知识——指针(3)
c语言·开发语言·c++·经验分享·笔记·学习·算法
无限进步_11 小时前
【C++】大数相加算法详解:从字符串加法到内存布局的思考
开发语言·c++·windows·git·算法·github·visual studio
C+-C资深大佬11 小时前
C++ 数据类型转换是如何实现的?
开发语言·c++·算法
oioihoii12 小时前
回归测试:软件演进中的质量守护神与实践全指南
c++
十五年专注C++开发13 小时前
CMake基础: 在release模式下生成调试信息的方法
linux·c++·windows·cmake·跨平台构建
点云SLAM13 小时前
C++(C++17/20)最佳工厂写法和SLAM应用综合示例
开发语言·c++·设计模式·c++实战·注册工厂模式·c++大工程系统
Q741_14713 小时前
C++ 队列 宽度优先搜索 BFS 力扣 662. 二叉树最大宽度 每日一题
c++·算法·leetcode·bfs·宽度优先
csdn_aspnet14 小时前
C++跨平台开发:工程难题与解决方案深度解析
c++
余衫马14 小时前
在Win10下编译 Poppler
c++·windows·qt·pdf·poppler