Skip to content

VSCode ROS 开发指南 (Clang)

约 1 分钟阅读 0%

编辑器

Visual Studio Code

语言服务

clangd (LLVM)

适用系统

Ubuntu 18.04+

难度

中级

在 VSCode 中的 “C/C++” 插件在开发 ROS(C++)时显得力不从心:

❌ 头文件索引差

CMakeList 写明了各种库,还需要在 cpp_configuration 中手动添加路径

❌ 静态检查失效

由于没有良好的索引,静态代码检查完全无法工作

❌ Quick-fix 无用

只能傻傻的提示一下可能的错误


以 Ubuntu 18.04 作为示例:

  1. 安装 clang

    Terminal window
    sudo apt install clang
    # 考虑到 ubuntu 源更新,其名称可能变为:clang-10
  2. 安装 clangd

    Terminal window
    sudo apt install clangd-10

    创建软链接让 VSCode 插件能找到:

    Terminal window
    sudo ln -s /usr/bin/clangd-10 /usr/bin/clangd
  3. 安装 clang-format(可选)

    Terminal window
    sudo apt install clang-format

在项目根目录下添加 .clang-format 文件:

点击展开完整配置
Language: Cpp
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
FixNamespaceComments: true
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 4
UseTab: Never

clangd

必装

作者:LLVM

CMake

必装

作者:twxs

Clang-Format

可选

作者:Xaver Hellauer

ext


  1. 设置编译命令路径

    在 VSCode 插件中找到 clangd 设置,在 “Clangd: Arguments” 中添加:

    --compile-commands-dir=${workspaceFolder}/build

    clangd-setting

  2. 配置工作空间编译参数

    Terminal window
    catkin config --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
    catkin build
  3. 验证配置

    检查 build/ 目录下是否有 compile_commands.json 文件。


finish

这样就可以愉快地使用 clangd 的特性进行 ROS 开发了!


发现问题?欢迎在 GitHub 上直接编辑此文档。