Skip to content

Commit 0a5f4e7

Browse files
Merge pull request #45 from neuralnet-cpp-dev/dev
clangd集成
2 parents 08cf445 + 8ae2c4f commit 0a5f4e7

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎.clangd‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# clangd 配置:指向 CMake 导出的编译数据库。
2+
# 前提:配置时带 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON(或用 CMakePresets,
3+
# 已默认打开),生成 build/compile_commands.json。
4+
# 若用 preset 构建(binaryDir = build/<preset名>),把下行改成对应目录,
5+
# 例如 CompilationDatabase: build/dev。
6+
CompileFlags:
7+
CompilationDatabase: build

‎CMakeLists.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ cmake_minimum_required(VERSION 3.30)
1212
# 它只是元数据——发版时同步到最新 tag,实际版本以 git tag / gh release 为准)
1313
project(neuralnet.cpp VERSION 1.14.1 LANGUAGES CXX)
1414

15+
# ── IDE / clangd:导出 compile_commands.json(仅顶层工程时)───────────────
16+
# include 路径(include/ + extern/*/include + build/generated)全部来自
17+
# target_include_directories,clangd 没有编译数据库就解析不到
18+
# <neuralnet.cpp/xxx.hpp>。Ninja/Makefile 生成器有效,MSVC/Xcode 忽略;
19+
# 嵌入消费方是否导出由其顶层工程决定,这里不越权。
20+
if(PROJECT_IS_TOP_LEVEL)
21+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
22+
endif()
23+
1524
# ══════════════════════════════════════════════════════════════════════════
1625
# 作为库被消费(add_subdirectory / FetchContent 嵌入 3rd_party)的门控
1726
# ══════════════════════════════════════════════════════════════════════════

‎docs/development/01-cmake-integration.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ tokenizer_infer,dataset_gen,dataset_convert}`,另有 `layer_bench`(性能)
188188
**已知构建坑**:链接报 `permission denied`(`build/` 下 exe 正在运行被占用 → 构建中止)
189189
→ 追加 `-- -k 0` 让 ninja 跳过失败目标继续建其余目标(被占用目标仍需关掉进程后重链)。
190190

191+
### IDE(clangd)如何解析 `<neuralnet.cpp/xxx.hpp>`
192+
193+
include 路径全部来自 `target_include_directories`(`include/` + `extern/*/include` +
194+
`build/generated`),clangd 必须拿到编译数据库才能解析。顶层配置时 CMake 自动
195+
导出 `build/compile_commands.json`(`CMAKE_EXPORT_COMPILE_COMMANDS`,仅 Ninja/Makefile
196+
生成器有效);仓库根的 `.clangd` 已指向 `build/`,clangd 打开任意头/源文件即可。
197+
用 preset 构建时编译数据库在 `build/<preset名>/`,把 `.clangd` 的
198+
`CompilationDatabase` 改成对应目录(或传 `--compile-commands-dir`)。
199+
改完 CMake 后记得重新 configure 一次,让数据库里的 `-I` 与目标保持同步。
200+
191201
---
192202

193203
## 8. 编译耗时

‎scripts/doc_audit.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
'--test-dir', '--parallel', '--target-env', '--dtype', '--adam-eps', '--device',
4848
'--task', '--compress', '--list', '--image', '--list-backends', '--summary', '--include',
4949
'--preset', '--workflow', '--list-presets', '--output-on-failure',
50+
# clangd 自带选项(IDE 集成文档用):clangd --compile-commands-dir <build>
51+
'--compile-commands-dir',
5052
# git 自带选项(文档命令用):git clang-format --diff / clone --recurse-submodules / tag --tags
5153
'--diff', '--recurse-submodules', '--tags',
5254
}

0 commit comments

Comments
 (0)