tags:

views:

152

answers:

5

How to count the number of global variables in C++ with a program that I can run with Grep?

+4  A: 

Grep has no knowledge of the syntax or the grammar; it operates on lines. I don't think this is possible.

Here's a snippet of some code I'm working on:

int count;

Can you tell me if it's global?

Jurily
More fun: `int foo(bar);`.
MSalters
A: 

You might be able to grep something in the artifacts of compilation such as listing files or object files.

Tony van der Peet
Or the map file!
Tony van der Peet
+5  A: 

A better method is to have your compiler print a map file. Most map files list all the global variables and their locations. If you're lucky, the map file may even indicate which translation unit the global variable belongs to.

Thomas Matthews
A: 

Hmmm...Have you considered using something like cflow? You can get the GNU's version of cflow, the output, can then be greppable?

Hope this helps, Best regards, Tom.

tommieb75
A: 

Anybody kwnows a cflow equivalent for C++?

Daniel Pinyol