开发者

List global variables in a C program

Is there a tool around that will list all the global variables in a C program? More specifically, is there a simple commandline tool that will do this, i.e. not a hea开发者_StackOverflow中文版vyweight IDE, CASE, graphical toolkit system etc., but just something that can be run like foo *.c?


ctags -R -x --sort=yes --c-kinds=v --file-scope=no file "c:\my sources" > c:\ctagop.txt


If you happen to compile the file on most unixes you have nm that just lists you all linker symbols. These symbols are classified in different groups (a bit platform dependent) so you should easily find out which the variables are.


Try ctags. Or, gcc with -aux-info. There is also gccxml and libclang but those two aren't very simple.


to list all globals (linux/gnu) for program 'foo': if the program is compiled with -g (gcc -g -o foo main.c) nm foo | grep " B " | sed -e "s/^[^B]*B//g" will list all globals, nm lists the objects, grep seperates out the global variables, and sed cleans up the presentation.

Actually most stuff I compile is with Makefiles so it is CFLAGS=-g -pg -Wextra in the Makefile, so the executable can be scanned until a 'make dist'

user1717828 on stackechange provided the grep template

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜