Search code examples
cclang

Get all the functions' names from c/cpp files


For example, there is a C file a.c, there are three functions in this file: funA(), funB() and funC().

I want to get all the function names from this file.

Additionally, I also want to get the start line number and end line number of each function.

Is there any solution?

Can I use clang to implement it?


Solution

  • One solution that works well for the job is cproto. It will scan source files (in K&R or ANSI-C format) and output the function prototypes. You can process entire directories of source files with a find command similar to:

    find "$dirname" -type f -name "*.c" \
    -exec /path/to/cproto -s \
    -I/path/to/extra/includes '{}' >> "$outputfile" \;
    

    While the cproto project is no longer actively developed, the cproto application continues to work very, very well. It provides function output in a reasonable form that can be fairly easily parsed/formatted as you desire.

    Note: this is just one option based on my use. There are many others available.