Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Variable identification in C code base

Tags:

c

linker

I have a C code base and need to programmatically identify all of the global variables used. I think that gcc can be made to produce a memory map file but don't know how and this might let me see which variables are global? Any help would be appreciated.

Cheers

like image 918
Howard May Avatar asked Nov 26 '25 10:11

Howard May


2 Answers

You can extract such information from your object files with nm.

nm *.o | grep OBJT | grep GLOB

EDIT Command above is for Solaris' nm (SUNWbtool package). For portability, nm has a parameter to choose the output format:

nm -f [posix|bsd|sysv] *.o
like image 171
philant Avatar answered Nov 28 '25 00:11

philant


The option to output memory map is -M with the linker, so to get it from gcc you have to use gcc .... -Xlinker -M.

Another way to to get these is to use the ctags program. It can not only tag the available functions, but also the available global variables (and does not collect the statics, this is different to memory map). As it doesnt compile everything this should also be faster than the gcc approach (if you have to compile, you would get that of course for free).

like image 25
flolo Avatar answered Nov 28 '25 01:11

flolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!