Python clang不搜索系统包含的路径
Python clang does not search system include paths
从Python中使用libclang时,它似乎不会自动搜索系统的包含路径。
有没有可靠的方法来获得这些路径?我不喜欢硬编码路径,因为我正在编写将在各种UNIX系统上运行的代码。
例如,给定test.cpp
#include <stdio.h>
int main()
{
puts("Hello, world!");
}
和测试.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自动使用它们,那就太好了。
这个问题已经存在一段时间了,所以我将尝试自己回答
似乎连Clang本身也大多使用硬编码路径。
它枚举候选路径,并添加适合当前上下文的路径。这可以在clang/lib/Frontend/IntHeaderSearch.cpp中看到。例如,
AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
"i686-apple-darwin10", "", "x86_64", triple);
AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
"i686-apple-darwin8", "", "", triple);
// ...
对于Linux,此代码有以下注意事项:
llvm_unreachable("Include management is handled in the driver.");
在clang/lib/Driver/
下,我们可以在ToolChains.cpp
、CrossWindowsToolChain.cpp
和MinGWToolChain.cpp
等文件中找到更多这样的路径。
我希望InitHeaderSearch.cpp
中的代码能够通过libclang公开给Python。
相关文章:
- 如何使用cppcheck处理半相对包含路径
- 如何确定包含路径的来源?
- 生成文件缺少包含路径 尽管路径存在并已定义
- 更改包含路径
- 在 Android Studio 中添加 C/C++ 的包含路径
- 如何设置 CMake 来构建包含路径包含的代码,如 #include < libdir/lib>?
- 在Visual Studio Code(.json配置)中设置相对于工作区路径的c / c ++项目的包含路径
- 如何在 pybind11 项目中设置包含路径
- CLion 不索引任何外部包含路径
- VS代码无法识别包含路径
- 使用相对标头包含路径重新分析 ASTUnit 失败
- 如何在忍者上添加包含路径,并为VS 2019添加叮当
- 使用 glibc 2.7 包含路径编译失败
- 通过程序代码行设置C++包含路径
- Visual Studio添加真正"global"默认包含路径
- target_include_directories生成的包含路径
- C++'Undefined reference to'错误,尽管包含路径中的头文件中存在函数定义
- 如何从 qmake 中的包含路径中删除项目的根目录
- 是什么让 clang 在包含路径的子目录中看起来?
- 如果库的包含路径设置不符合预期,如何引发编译错误