clang-tidy无法检测compile_commands.json,如何修复该问题?
修复clang-tidy无法加载compile_commands.json的问题
问题详情
我的compile_commands.json内容如下:
{ "directory": "/data/data/com.termux/files/home/cpulator", "file": "allocator.cc", "output": "allocator.o", "arguments": ["/data/data/com.termux/files/usr/bin/clang-16", "-xc++", "--sysroot=/data/data/com.termux/files", "events.cc", "-o", "events.o", "--target=arm-altera-eabi", "-nostdlib", "-Wall", "-Wextra", "-Wpedantic", "-c", "-O0", "-gdwarf-4", "-fno-exceptions", "-fno-rtti", "-fno-inline", "--target=armv4t-altera-unknown-eabi"]}
执行clang-tidy allocator.cc时触发报错:
Error while trying to load a compilation database: Could not auto-detect compilation database from directory "/data/data/com.termux/files/home/cpulator" No compilation database found in /data/data/com.termux/files/home/cpulator or any parent directory fixed-compilation-database: Error while opening fixed database: No such file or directory json-compilation-database: Expected array.
修复步骤
1. 修正JSON格式(核心问题)
clang-tidy要求compile_commands.json的根结构必须是JSON数组,而非单个JSON对象。将现有内容用[]包裹即可:
[ { "directory": "/data/data/com.termux/files/home/cpulator", "file": "allocator.cc", "output": "allocator.o", "arguments": ["/data/data/com.termux/files/usr/bin/clang-16", "-xc++", "--sysroot=/data/data/com.termux/files", "events.cc", "-o", "events.o", "--target=arm-altera-eabi", "-nostdlib", "-Wall", "-Wextra", "-Wpedantic", "-c", "-O0", "-gdwarf-4", "-fno-exceptions", "-fno-rtti", "-fno-inline", "--target=armv4t-altera-unknown-eabi"] } ]
2. 确认文件位置
确保compile_commands.json存放在项目根目录/data/data/com.termux/files/home/cpulator下,clang-tidy会自动在当前目录及父目录检索该文件。如果文件放在其他路径,可通过-p参数指定:
clang-tidy -p /path/to/compile_commands_dir allocator.cc
3. 修正编译命令中的文件不匹配(可选)
注意到你arguments里指定的输入文件是events.cc,但实际要分析的是allocator.cc,这会导致clang-tidy无法匹配正确的编译规则,建议修正:
"arguments": ["/data/data/com.termux/files/usr/bin/clang-16", "-xc++", "--sysroot=/data/data/com.termux/files", "allocator.cc", "-o", "allocator.o", "--target=arm-altera-eabi", "-nostdlib", "-Wall", "-Wextra", "-Wpedantic", "-c", "-O0", "-gdwarf-4", "-fno-exceptions", "-fno-rtti", "-fno-inline", "--target=armv4t-altera-unknown-eabi"]
内容的提问来源于stack exchange,提问作者t14njin
相关产品推荐
相关产品推荐

