Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the range (i.e., the line number) of all functions in a file in C?

I want to get both the beginning and ending line numbers of all functions in a file in C. Does any one know that whether there is a easy-to-use tool in Linux for this purpose?

like image 953
user1038013 Avatar asked Oct 29 '25 22:10

user1038013


1 Answers

$ ctags -x --c-kinds=f filename.c

This only gives the starting line of each function, but perhaps that is good enough.

If the code was written using fairly common conventions, the function should end with a single line containing } in the first column, so it is fairly easy to get the last line given the first:

awk 'NR > first && /^}$/ { print NR; exit }' first=$FIRST_LINE filename.c
like image 142
William Pursell Avatar answered Oct 31 '25 13:10

William Pursell