Python clang不搜索系统包含路径 [英] Python clang does not search system include paths

查看:201
本文介绍了Python clang不搜索系统包含路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用libclang从Python,它似乎不会自动搜索系统的包含路径。



有没有可靠的方法来获得这些路径?我不喜欢硬编码路径,因为我编写的代码将运行在各种UNIX系统上。



例如,给定test.cpp

  #include< stdio.h> 

int main()
{
puts(Hello,world!
}

和test.py

  from clang.cindex import索引

tu = Index.create()。parse(None,[test.cpp])
print(list(tu.diagnostics))

运行 python test.py 将打印:

  [<诊断严重性4,location< SourceLocation file'test.cpp' ,line 1,
column 10>,拼写'stdio.h'file not found>]

当然,我可以通过执行

  $ clang -v -E test.cpp来找到系统包含路径

并添加 - Isome / path parse 参数列表,即

  args = [ I / Applications / [...],test.cpp] 



但是,这是不可移植的,如果我可以通过编程方式得到自动使用它们。 $ b

解决方案

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

它似乎甚至Clang本身使用大多数硬编码的路径。



它枚举候选路径,并添加适合当前上下文。这可以在 clang / lib / Frontend / InitHeaderSearch.cpp 。例如,

  AddGnuCPlusPlusIncludePaths(/ usr / include / c ++ / 4.2.1,
i686- darwin10,,x86_64,triple);
AddGnuCPlusPlusIncludePaths(/ usr / include / c ++ / 4.0.0,
i686-apple-darwin8,,,triple);

// ...

  llvm_unreachable(包括管理在驱动程序中处理。 

clang / lib / Driver / 可以在文件中找到更多的这些路径,例如 ToolChains.cpp CrossWindowsToolChain.cpp MinGWToolChain.cpp



我所希望的是 InitHeaderSearch.cpp 将通过libclang暴露给Python。


When using libclang from Python, it doesn't seem to automatically search the system's include paths.

Is there a reliable way to get these paths? I don't like hardcoding paths as I'm writing code that will run on a variety of UNIX systems.

For example, given test.cpp

#include <stdio.h>

int main()
{
  puts("Hello, world!");
}

and test.py

from clang.cindex import Index

tu = Index.create().parse(None, ["test.cpp"])
print(list(tu.diagnostics))

running python test.py will print:

[<Diagnostic severity 4, location <SourceLocation file 'test.cpp', line 1, 
 column 10>, spelling "'stdio.h' file not found">]

Of course, I can find the system include paths by doing

$ clang -v -E test.cpp

and adding "-Isome/path" to the parse argument-list, i.e.

args = ["-I/Applications/[...]", "test.cpp"]

That actually works and produces no errors.

However, this isn't portable, and it would be really nice if I could programmatically get clang to automatically use them.

解决方案

This question has been up for some time, so I'll attempt to answer it myself.

It seems that even Clang itself uses mostly hardcoded paths.

It enumerates candidate paths and adds the ones that fit under the current context. This can be seen in clang/lib/Frontend/InitHeaderSearch.cpp. E.g.,

AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
                           "i686-apple-darwin10", "", "x86_64", triple);
AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
                           "i686-apple-darwin8", "", "", triple);

// ...

For Linux, this code has the notice:

llvm_unreachable("Include management is handled in the driver.");

Under clang/lib/Driver/ we can find more of these paths in files such as ToolChains.cpp, CrossWindowsToolChain.cpp and MinGWToolChain.cpp.

What I was hoping for was that the code in InitHeaderSearch.cpp would be exposed to Python through libclang.

这篇关于Python clang不搜索系统包含路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆