我正在尝试编写一个小的构建脚本 - 并且想要确定包含是否包含系统.所以我希望g ++告诉我它正在使用的包含路径.
cpp -v似乎是最好的镜头,但它并没有给我C++路径.
所以我尝试过:
g++ -Xpreprocessor -v
Run Code Online (Sandbox Code Playgroud)
哪个不能正常工作 - g ++捕获-v为它自己的详细输出.
谢谢,奥利弗
当从 Python 使用 libclang 时,它似乎不会自动搜索系统的包含路径。
有没有可靠的方法来获取这些路径?我不喜欢硬编码路径,因为我正在编写将在各种 UNIX 系统上运行的代码。
例如,给定 test.cpp
#include <stdio.h>
int main()
{
puts("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)
和测试.py
from clang.cindex import Index
tu = Index.create().parse(None, ["test.cpp"])
print(list(tu.diagnostics))
Run Code Online (Sandbox Code Playgroud)
运行python test.py将打印:
[<Diagnostic severity 4, location <SourceLocation file 'test.cpp', line 1,
column 10>, spelling "'stdio.h' file not found">]
Run Code Online (Sandbox Code Playgroud)
当然,我可以通过以下方式找到系统包含路径
$ clang -v -E test.cpp
Run Code Online (Sandbox Code Playgroud)
并添加"-Isome/path"到parse参数列表中,即
args = ["-I/Applications/[...]", "test.cpp"]
Run Code Online (Sandbox Code Playgroud)
这确实有效并且不会产生任何错误。
然而,这不是可移植的,如果我能够以编程方式让 clang 自动使用它们,那就太好了。
我有一些代码(从这里和这里获取和改编),它使用libclang 解析Python(Widnows)中的C++ 源文件并获取其所有声明语句,如下所示:
import clang.cindex
def parse_decl(node):
reference_node = node.get_definition()
if node.kind.is_declaration():
print(node.kind, node.kind.name,
node.location.line, ',', node.location.column,
reference_node.displayname)
for ch in node.get_children():
parse_decl(ch)
# configure path
clang.cindex.Config.set_library_file('C:/Program Files (x86)/LLVM/bin/libclang.dll')
index = clang.cindex.Index.create()
trans_unit = index.parse(r'C:\path\to\sourcefile\test.cpp', args=['-std=c++11'])
parse_decl(trans_unit.cursor)
Run Code Online (Sandbox Code Playgroud)
对于以下 C++ 源文件 ( test_ok.cpp):
/* test_ok.cpp
*/
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
int main (int argc, char *argv[]) {
int linecount …Run Code Online (Sandbox Code Playgroud)