如何计算 CPP 文件中的所有全局变量
how to count all global variables in the cpp file
是否存在任何cpp代码解析器来解决此问题?例如
// B.cpp : Defines the entry point for the console application.
//
#include<iostream>
#include<vector>
#include<algorithm>
size_t N,M;
const size_t MAXN = 40000;
std::vector<std::pair<size_t,size_t> > graph[MAXN],query[MAXN],qr;
size_t p[MAXN], ancestor[MAXN];
bool u[MAXN];
size_t ansv[MAXN];
size_t cost[MAXN];
size_t find_set(size_t x){
return x == p[x] ? x : p[x] = find_set(p[x]);
}
void unite(size_t a, size_t b, size_t new_ancestor){
}
void dfs(size_t v,size_t ct){
}
int main(int argc, char* argv[]){
return 0;
}
这个文件有10个全局变量:祖先,ansv,成本,图,M,N,p,qr,查询,u
您可以使用以下 shell 命令调用编译器并计算导出的全局变量:
$ g++ -O0 -c B.cpp && nm B.o | grep ' B ' | wc -l
10
如果删除行数,则会得到他们的名字
$ g++ -O0 -c B.cpp && nm B.o | egrep ' [A-Z] ' | egrep -v ' [UTW] '
00000004 B M
00000000 B N
00111740 B ancestor
00142480 B ansv
00169580 B cost
00000020 B graph
000ea640 B p
000ea620 B qr
00075320 B query
00138840 B u
让我们看看这是如何工作的。
g++ -O0 -c B.cpp
:这调用编译器而不进行优化,因此输出(默认情况下B.o
)几乎是没有删除标识符的已编译文件。nm B.o
:调用 nm 一个工具(引用自链接)"列出对象文件中的符号"。例如,如果"交易品种在未初始化的数据部分中",则存在"B"。我们希望有全局值(表示大写),但不是 U、T 或 W。这就是 grep 所做的。
相关文章:
- 在 elf 文件中查找全局变量的位置
- 访问另一个.cpp文件中的.cpp文件中定义的全局变量
- 在主循环外部多个文件上定义全局变量
- 查找声明全局变量的文件
- 通过其他文件中的另一个全局变量使用类的全局实例
- 如何管理多个文件、全局变量和定义
- C 使用c库,其中包含标题文件中定义的全局变量
- 如何在可执行文件中从.so访问全局变量
- cpp 文件中的全局变量
- 在blackberry cascades 10中访问多个qml文件中的全局变量
- 将所有常用的全局变量移到一个头文件中,以减少#includes的数量,这是一种好的做法吗
- 如何使用在源文件内的头文件中声明的全局变量
- 生成文件:在全局变量初始化之前通过"make all"命令运行 bash 脚本
- 跨多个文件的c++全局变量
- 包含文件会导致全局变量出现多个定义错误
- 如何创建一个具有全局变量的 lib 文件,这些全局变量应该将其信息共享给另一个项目
- 跨多个.cpp文件共享全局变量(visual studio 2015)
- 在头文件中包含全局变量
- 如何声明和定义全局变量,以便从所有头文件/源文件中正确访问它们
- 头文件中的全局变量导致c++重定义错误