Files
VSAG_SENTRY_SIM_27RMUL/.vscode/README.md
T

2.7 KiB
Raw Blame History

.vscode/

VS Code 的工作区配置(只影响编辑器体验,删掉不影响仿真运行)。

文件 说明
settings.json clangd 设成唯一的 C++ 智能感知引擎;xacro/urdf/sdf 关联成 XML
c_cpp_properties.json cpptools 的兜底配置(当前被禁用,留着备用)

C++ 智能感知怎么工作

用 clangd,它读工作区根的 compile_commands.json(真实编译命令), 所以 include 路径、宏定义、C++ 标准全部自动跟构建保持一致,不用手写。

colcon build(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)
    ↓ 每个包生成 build/<包>/compile_commands.json
tools/gen_compdb.sh 合并
    ↓
compile_commands.json(工作区根)
    ↓
clangd 读取

⚠️ 最常踩的坑:报「xxx.hpp file not found」

现象:

'sentry_nav_core/waypoint_sequencer.hpp' file not found
Too many errors emitted, stopping now        [-ferror-limit=]

原因:compile_commands.json 是旧的。clangd 只读它,不会自己去看 CMakeLists.txt。 只要做过下面任何一件事,这个文件就过期了:

  • 新增 / 重命名 / 删除 .cpp
  • 改 CMakeLists.txt 的 add_library / include_directories / target_include_directories
  • 新增 #include 指向一个新的目录(比如这次的 sentry_nav_core)
  • 换 C++ 标准

修复(两步,缺一不可):

cd ~/Gazebo_projects/sentry_nav_dev
tools/gen_compdb.sh          # 重新编译 + 合并 compdb

然后在 VS Code 里:

Ctrl+Shift+P  →  clangd: Restart language server

只跑 colcon build 不跑 gen_compdb.sh 是不够的 —— 它只更新 build/<包>/compile_commands.json,工作区根那份不会变。

验证 compdb 是否正确

# 看根 compdb 里有没有你的新文件
python3 -c "
import json
for e in json.load(open('compile_commands.json')):
    print(e['file'].split('sentry_nav_dev/')[-1])
"

# 更彻底:直接用 clangd 解析一遍(应该输出 All checks completed, 0 errors)
clangd --check=src/sentry_nav/src/patrol.cpp --compile-commands-dir=.

兜底配置(clangd.fallbackFlags)

只在你单独打开一个头文件、而它不在任何编译单元里时才生效:

"-std=c++23",
"-I/opt/ros/lyrical/include",
"-I${workspaceFolder}/src/sentry_nav_core/include",
"-I${workspaceFolder}/src/sentry_nav/include"

真正的构建仍然用命令行

source /opt/ros/lyrical/setup.bash
colcon build --symlink-install

cmake.sourceDirectory 指向 src/sentry_sim 是 CMake Tools 插件需要的一个值, 跟 colcon 构建无关,被插件来回改写可以忽略。