在 Python 中使用 libclang 时,它似乎不会自动搜索系统的包含路径。
有没有可靠的方法来获得这些路径?我不喜欢硬编码路径,因为我正在编写将在各种 UNIX 系统上运行的代码。
例如,给定 test.cpp
#include <stdio.h>
int main()
{
puts("Hello, world!");
}
和 test.py
from clang.cindex import Index
tu = Index.create().parse(None, ["test.cpp"])
print(list(tu.diagnostics))
运行python test.py
将打印:
[<Diagnostic severity 4, location <SourceLocation file 'test.cpp', line 1,
column 10>, spelling "'stdio.h' file not found">]
当然,我可以通过做找到系统包含路径
$ clang -v -E test.cpp
并添加"-Isome/path"
到parse
参数列表中,即
args = ["-I/Applications/[...]", "test.cpp"]
这实际上有效并且不会产生错误。
但是,这不是可移植的,如果我能以编程方式让 clang 自动使用它们,那就太好了。