繁体   English   中英

Python clang不搜索系统包含路径

[英]Python clang does not search system include paths

当从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自动使用它们,那将是非常好的。

这个问题已经有一段时间了,所以我会尝试自己回答。

似乎即使是Clang本身也主要使用硬编码路径。

它枚举候选路径并添加适合当前上下文的路径。 这可以在clang / lib / Frontend / InitHeaderSearch.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.cppCrossWindowsToolChain.cppMinGWToolChain.cpp等文件中找到更多这些路径。

我希望的是, InitHeaderSearch.cpp中的代码将通过libclang向Python公开。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM