libclang:添加编译器系统包含路径(Windows中的Python)

libclang: add compiler system include path (Python in Windows)

本文关键字:Windows 中的 Python 路径 包含 添加 编译器 系统 libclang      更新时间:2023-10-16

根据这个问题和Andrew的建议,我正在尝试让liblang添加编译器系统包含路径(在Windows中),以便我的Python代码

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:pathtosourcefiletest.cpp', args=['-std=c++11'])
parse_decl(trans_unit.cursor)

完全解析C++源文件,如

/* test.cpp
*/
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;
void readfunction(vector<double>& numbers, ifstream& myfile)
{
  double number;
  while (myfile >> number) {
  numbers.push_back(number);}
}
double meanfunction(vector<double>& numbers)
{
  double total=0;
  vector<double>::const_iterator i;
  for (i=numbers.begin(); i!=numbers.end(); ++i) {
  total +=*i; }
  return total/numbers.size();
}

现在,如果没有编译器系统包含适当设置的路径(使用Windows),我会得到以下输出:

CursorKind.USING_DIRECTIVE USING_DIRECTIVE 8 , 17 std
CursorKind.VAR_DECL VAR_DECL 10 , 6 readfunction
Process finished with exit code 0
<Diagnostic severity 4, location <SourceLocation file 'test.cpp', line 3, column 10>, spelling "'iostream' file not found">

不幸的是,我无法理解这种方法(Python和Clang中的新方法),也无法理解如何在我的Python代码中实现这种解决方案。

我也尝试过ccsyspath,但我不具备"针对窗口进行调整"的技能。

有人知道如何解决这个问题吗?

在Windows中,要向路径添加内容,必须执行以下操作:

  1. 系统属性
  2. 高级
  3. 环境变量
  4. 从表中选择"路径"
  5. 第一个"编辑"按钮
  6. 添加到要添加到路径的可执行文件的位置

希望这能有所帮助!


(如果我误解了你的问题,请告诉我,我还是堆栈溢出的新手。谢谢!)