我正在尝试编写一个小的构建脚本 - 并且想要确定包含是否包含系统.所以我希望g ++告诉我它正在使用的包含路径.
cpp -v似乎是最好的镜头,但它并没有给我C++路径.
所以我尝试过:
g++ -Xpreprocessor -v
Run Code Online (Sandbox Code Playgroud)
哪个不能正常工作 - g ++捕获-v为它自己的详细输出.
谢谢,奥利弗
我从这个网站下载了Clang 3.6.2,并尝试使用Windows下的Code :: Blocks进行设置.不幸的是,由于它不知道在哪里,它无法编译一个简单的"hello world"程序iostream
.
查看安装文件夹,它似乎没有包含标准库.为什么?我怎么得到它?
按照这个问题和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)
Run Code Online (Sandbox Code Playgroud)
到完全 解析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 …
Run Code Online (Sandbox Code Playgroud) 我试图通过Python中的libclang解析C++源文件时找到(行和列位置)特定函数声明的所有引用.
例如:
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main ()
{
int z, q;
z = addition (5,3);
q = addition (5,5);
cout << "The first result is " << z;
cout << "The second result is " << q;
}
Run Code Online (Sandbox Code Playgroud)
所以,对于上面的源文件,我想要addition
第5行的函数声明,我希望find_all_function_decl_references
(见下文)返回第addition
15行和第16行的引用.
我试过这个(改编自这里)
import clang.cindex
import ccsyspath
index = clang.cindex.Index.create()
translation_unit = index.parse(filename, args=args)
for node in …
Run Code Online (Sandbox Code Playgroud) 经过一些研究和几个问题,我最终探索了libclang库,以便用Python解析C ++源文件。
给定C ++源代码
int fac(int n) {
return (n>1) ? n?fac(n?1) : 1;
}
for (int i = 0; i < linecount; i++) {
sum += array[i];
}
double mean = sum/linecount;
Run Code Online (Sandbox Code Playgroud)
我试图将标记识别 fac
为函数名称,n
变量名称,i
变量名称,mean
变量名称以及每个位置。我有兴趣最终将它们标记化。
我已经阅读了一些非常有用的文章(eli的,Gaetan的),以及一些堆栈溢出问题35113197和13236500。
但是,鉴于我是Python的新手,并且努力理解libclang的基础知识,因此,我非常感谢一些示例代码块,这些代码实现了以上内容,供我学习和理解。