简体   繁体   English

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

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

Following this question and Andrew's suggestions, I am trying to have liblang add the compiler system include paths (in Windows) in order for my Python code 按照这个问题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:\path\to\sourcefile\test.cpp', args=['-std=c++11'])

parse_decl(trans_unit.cursor)

to completely parse C++ source files like this one 完全 解析像这样的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();

}

Now, without the compiler system include path set up appropriately (using Windows), I get the following output: 现在,如果没有编译器系统包含适当的路径设置(使用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">

Unfortunately, I cannot make sense (new in Python and Clang) of this approach or how to implement this solution in my Python code. 不幸的是,我无法理解这种方法 (Python和Clang中的新内容)或如何在我的Python代码中实现此解决方案

I have also tried ccsyspath , but I do not have the skills to 'adjust it for windows'. 我也尝试了ccsyspath ,但我没有“为Windows调整它”的技能。

Anyone knows how to solve this issue? 谁知道如何解决这个问题?

In Windows to add something to the path you must do the following: 在Windows中,要在路径中添加内容,您必须执行以下操作:

  1. System properties 系统属性
  2. Advanced 高级
  3. Environment variables 环境变量
  4. Select "path" from the table 从表中选择“路径”
  5. First "edit" button 首先“编辑”按钮
  6. Add in the location of the executable that you are trying to add to path 添加您尝试添加到路径的可执行文件的位置

Hope this helps! 希望这可以帮助!


(Please tell me if I misunderstood your question, I am still new to stack overflow. Thanks!) (请告诉我,如果我误解了你的问题,我仍然是新的堆栈溢出。谢谢!)

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

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