我目前的文件结构是:
├── common
│ └── example.cc
├── compile_commands.json
├── include
└── common
└── example.hh
example.hh
:将其保留为空白
example.cc
#include "common/example.hh"
int main() {
return 0;
}
compile_commands.json
[
{
"directory": "/home/user/project",
"file": "/home/user/project/common/example.cc",
"arguments": [
"/usr/bin/clang++",
"-I /home/user/project/include",
"-o example",
"/home/user/project/common/example.cc"
],
"output": "example"
}
]
当打开example.cc
时,它会产生错误:
'common/example.hh' file not found clang(pp_file_not_found)
我直接运行这个命令,它运行得很好:
$ /usr/bin/clang++ -I /home/user/project/include -o example /home/user/project/common/example.cc
环境信息:
$ clang++ --version
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
vscode: 1.47.3
vscode-clangd: 0.1.7
发布于 2022-07-08 05:59:42
问题是,在"arguments"
字段中的compile_commands.json
条目中,"-I /home/user/project/include"
是一个单一参数。( "-o example"
也是如此,但在这种情况下不会造成任何损害。)
这意味着执行的命令将像您编写的那样:
$ /usr/bin/clang++ "-I /home/user/project/include" "-o example" /home/user/project/common/example.cc
(请注意"-I /home/user/project/include"
和"-o example"
的引号)。
如果运行该命令,则会产生相同的'common/example.hh' file not found clang(pp_file_not_found)
错误。基本上,引号使clang认为-I
的参数以一个空格字符开始,然后空间字符不会解析为路径。
解决方案是将命令行的每个令牌放置在"arguments"
的单独元素中。
[
{
"directory": "/home/user/project",
"file": "/home/user/project/common/example.cc",
"arguments": [
"/usr/bin/clang++",
"-I",
"/home/user/project/include",
"-o",
"example",
"/home/user/project/common/example.cc"
],
"output": "example"
}
]
之所以以这种方式解释"arguments"
,是因为格式规范 for compile_commands.json
将"arguments"
描述为:
参数:编译命令argv作为字符串列表。这应该运行翻译单元
file
的编译步骤。arguments[0]
应该是可执行的名称,例如clang++
。参数不应转义,而应准备传递给execvp()
。
基本上,"arguments"
应该是命令行参数,因为shell会解析它们并将它们传递给程序的argv
。由于参数是空格分隔的,单个参数可以包含内部空间的唯一方法是在命令行中引用该参数。
最后,我要指出的是,compile_commands.json
文件通常不是手工编写的,而是由工具(通常是项目的构建系统或其他相关工具)生成的。有关生成这些工具的常用工具列表,请参见https://clangd.llvm.org/installation#project-setup。
如果您必须手动编写一个compile_commands.json
,那么使用"command"
而不是"arguments"
可能更容易(例如,请参考等级库 ),这正是为了避免这样的问题。
https://stackoverflow.com/questions/63787624
复制相似问题