gen_compile_commands.sh

下面是为你的项目结构 量身定制 的完整脚本:

它会扫描所有 .cpp 文件,添加正确的 -I 包含目录,输出符合 clangd 要求的 compile_commands.json 文件。


✅ 请将以下内容保存为 gen_compile_commands.sh

bash 复制代码
#!/bin/bash

# 项目根目录
PROJECT_DIR=$(pwd)

# 所有的头文件包含路径(根据你的 tree.txt 自动生成)
INCLUDES="-IAlarm/include \
-ICondition/include \
-IControl/include \
-ICore/include \
-IDataLog/include \
-IIO/include \
-IInterlock/include \
-IRecipe/include \
-IRemoteAccess/include \
-ISetup/include"

# 其他通用编译选项(根据你的 make.txt 提取)
CXXFLAGS="-Wall -Wextra -g -O0 -DDEBUG -fPIC -std=c++98"

# 开始生成 JSON 文件
echo "[" > compile_commands.json

# 标记是否是第一个条目(用于避免最后多逗号)
first=1

# 遍历所有 .cpp 文件
find . -name "*.cpp" | while read file; do
  # 添加逗号(跳过第一个)
  if [ $first -eq 0 ]; then
    echo "," >> compile_commands.json
  fi
  first=0

  echo "  {" >> compile_commands.json
  echo "    \"directory\": \"$PROJECT_DIR\"," >> compile_commands.json
  echo "    \"command\": \"g++ $CXXFLAGS $INCLUDES -c $file\"," >> compile_commands.json
  echo "    \"file\": \"$file\"" >> compile_commands.json
  echo -n "  }" >> compile_commands.json
done

# 结束 JSON
echo "" >> compile_commands.json
echo "]" >> compile_commands.json

echo "✅ compile_commands.json 生成完毕,共包含 $(grep -c 'file' compile_commands.json) 个源文件。"

✅ 使用方法

  1. 将脚本保存为:

    bash 复制代码
    gen_compile_commands.sh
  2. 加可执行权限:

    bash 复制代码
    chmod +x gen_compile_commands.sh
  3. 在项目根目录运行它:

    bash 复制代码
    ./gen_compile_commands.sh
  4. 会在当前目录生成:

    复制代码
    ./compile_commands.json

✅ clangd 就绪!

打开 Neovim,确认 clangd 可以正确跳转:

  • gd 跳转定义
  • gr 查找引用
  • K 查看文档
  • <C-Space> 补全提示

如需要,我还可以帮你配置 .clangd.clang-format 文件,是否需要?

相关推荐
dingdingfish13 小时前
Bash 学习 - 第1章:Introduction
bash·shell·programming·introduction
pr_note2 天前
legality检查
shell·tcl
啥都不懂的小小白4 天前
Shell脚本编程入门:从零基础到实战掌握
前端·shell
dingdingfish7 天前
GNU Parallel 学习 - 第1章:How to read this book
bash·shell·gnu·parallel
似霰10 天前
Linux Shell 脚本编程——核心基础语法
linux·shell
似霰11 天前
Linux Shell 脚本编程——脚本自动化基础
linux·自动化·shell
偷学技术的梁胖胖yo12 天前
Shell脚本中连接数据库查询数据报错 “No such file or directory“以及函数传参数组
linux·mysql·shell
纵有疾風起21 天前
【Linux 系统开发】基础开发工具详解:软件包管理器、编辑器。编译器开发实战
linux·服务器·开发语言·经验分享·bash·shell
gis分享者23 天前
Shell 脚本中如何使用 here document 实现多行文本输入? (中等)
shell·脚本·document·多行·文本输入·here
柏木乃一23 天前
基础IO(上)
linux·服务器·c语言·c++·shell