都改用Xmake

This commit is contained in:
mzx committed 2026-11-17 21:47:13 +08:00
1 parent ec9332826a
commit d8f1e2226d
21 files changed
+355 -561

No files matched your search

+20 -17
View File
@@ -7,43 +7,47 @@ VS Code 的工作区配置(只影响编辑器体验,**删掉不影响仿真
| `settings.json` | clangd 设成唯一的 C++ 智能感知引擎;`xacro/urdf/sdf` 关联成 XML |
| `c_cpp_properties.json` | cpptools 的兜底配置(当前被禁用,留着备用) |
> 注意:这里**没有** `cmake.sourceDirectory`。本工程不使用 CMake / colcon,
> 那行配置已随迁移删除;如果 CMake Tools 插件又把它写回来,可以忽略。
## 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(工作区根)
xmake(仓库根)
↓ plugin.compile_commands.autoupdate 规则自动触发
compile_commands.json(工作区根,每次构建后重写)
↓
clangd 读取
```
**没有手工生成步骤** —— 以前那个 `tools/gen_compdb.sh`(合并 colcon 的分包产物)
已经随去 colcon 一起删掉了。
## ⚠️ 最常踩的坑:报「xxx.hpp file not found」
**现象**:
```
'sentry_nav_core/waypoint_sequencer.hpp' file not found
'sentry_nav/waypoint_sequencer.hpp' file not found
Too many errors emitted, stopping now [-ferror-limit=]
```
**原因**:`compile_commands.json` 是**旧的**。clangd 只读它,不会自己去看 `CMakeLists.txt`。
**原因**:`compile_commands.json` 是**旧的**。clangd 只读它,不会自己去看 `xmake.lua`。
只要做过下面任何一件事,这个文件就过期了:
- 新增 / 重命名 / 删除 `.cpp`
- 改 `CMakeLists.txt` 的 `add_library` / `include_directories` / `target_include_directories`
- 新增 `#include` 指向一个新的目录(比如这次的 `sentry_nav_core`)
- 改 `xmake.lua` 的 `add_files` / `add_includedirs` / `set_languages`
- 新增 `#include` 指向一个新的目录
- 换 C++ 标准
**修复**(两步,缺一不可):
```bash
cd ~/Gazebo_projects/sentry_nav_dev
tools/gen_compdb.sh # 重新编译 + 合并 compdb
xmake # 构建 + 自动重写根 compile_commands.json
```
然后在 VS Code 里:
@@ -52,13 +56,12 @@ tools/gen_compdb.sh # 重新编译 + 合并 compdb
Ctrl+Shift+P → clangd: Restart language server
```
> 只跑 `colcon build` 不跑 `gen_compdb.sh` 是**不够的** ——
> 它只更新 `build/<包>/compile_commands.json`,工作区根那份不会变。
> 只 `clangd: Restart` 不跑 `xmake` 是**不够的** —— 文件本身没变,clangd 读到的还是旧路径。
## 验证 compdb 是否正确
```bash
# 看根 compdb 里有没有你的新文件
# 看根 compdb 里有哪些编译单元(应该是 6 个:2 节点 + 2 核心 + 2 测试)
python3 -c "
import json
for e in json.load(open('compile_commands.json')):
@@ -82,9 +85,9 @@ clangd --check=src/sentry_nav/src/patrol.cpp --compile-commands-dir=.
## 真正的构建仍然用命令行
```bash
source /opt/ros/lyrical/setup.bash
colcon build --symlink-install
cd ~/Gazebo_projects/sentry_nav_dev
xmake # 编译
xmake test # 单元测试
```
> `cmake.sourceDirectory` 指向 `src/sentry_sim` 是 CMake Tools 插件需要的一个值,
> 跟 colcon 构建无关,被插件来回改写可以忽略。
> `sentry_sim` 里没有 C/C++ 源码,编辑器对它只剩 SDF/XML 高亮,不涉及编译。
+6 -9
View File
@@ -1,18 +1,15 @@
{
"cmake.sourceDirectory": "/home/mzxiang/Gazebo_projects/sentry_nav_dev/src/sentry_sim",
// ---- C++ 智能感知 ----
// 用 clangd 当唯一引擎:它直接读工作区根的 compile_commands.json,
// include 路径/宏定义全部来自真实编译命令,不会再漏 ROS 头文件或本工程的头文件。
//
// ⚠️ 改了 CMakeLists(新增 .cpp / 改 include 目录 / 加新包)之后,
// 必须重新生成编译数据库,否则 clangd 还在用旧路径,
// 表现就是 "xxx.hpp file not found" + "Too many errors emitted":
// 这份 compile_commands.json 由 xmake 在**每次构建后自动重写**
// (根 xmake.lua 里的 plugin.compile_commands.autoupdate 规则),
// 不需要任何手工生成步骤。
//
// cd ~/Gazebo_projects/sentry_nav_dev
// tools/gen_compdb.sh
//
// 然后在 VS Code 里 Ctrl+Shift+P → "clangd: Restart language server"。
// 改了源码结构、但编辑器还在报 "xxx.hpp file not found" 时,
// 跑一次 `xmake` 让它刷新,然后在 VS Code 里
// Ctrl+Shift+P → "clangd: Restart language server"。
"C_Cpp.intelliSenseEngine": "disabled",
// 不丢 --clang-tidy,避免 ROS 头文件里一堆代码风格告警糊满屏幕。
"clangd.arguments": [