如何计算cpp文件中的所有全局变量 [英] how to count all global variables in the cpp file

查看:207
本文介绍了如何计算cpp文件中的所有全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是否存在任何cpp代码解析器来解决这个问题?例如

Does it exist an any cpp code parser to solve this problem? For example

// 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个全局变量: ancestor ansv 费用图表 M / strong>, qr 查询 u

This file has 10 global variables : ancestor, ansv, cost, graph, M, N, p, qr, query, u

推荐答案

您可以使用以下shell命令调用编译器并计算导出的全局变量:

You could invoke the compiler and count the exported global variables with the following shell command:

$ g++ -O0 -c B.cpp && nm B.o | grep ' B ' | wc -l
10

如果删除行计数, p>

If you remove the line count, you get their names

$ 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

让我们看看它是如何工作的。

Let's see how this works.


  1. g ++ -O0 -c B.cpp :这会调用没有优化的编译器,使得输出(默认情况下 Bo )几乎是未删除标识符的编译文件。

  1. g++ -O0 -c B.cpp: This calls the compiler without optimizations such that the output (B.o by default) is pretty much the compiled file without removed identifiers.

nm Bo :电话 nm (从链接引用)从对象文件列出符号的工具。例如,符号在未初始化数据部分,则有一个B。

nm B.o: Calls nm a tool that (quote from link) "list symbols from object files". If, for example "the symbol is in the uninitialized data section", there is a "B".

我们想要有全局值但不是U,T或W.这是grep的作用。

We want to have global values (means uppercase) but not U, T or W. This is what the grep does.

这篇关于如何计算cpp文件中的所有全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆