Extracting C / C++ function prototypes

If you have universal-ctags (https://ctags.io), --_xformat option may be useful though you need sed and tr commands to get what you want.

$ cat input.c 
struct object *new_object (struct
                           /* COMMENT */
                           param
                           /* IGNORE ME */
                           *p)
{
    return NULL;
}
int main (void)
{
    return 0;
}
$ ./ctags -o - --kinds-C=f --kinds-C++=f -x --_xformat='%{typeref} %{name} %{signature};' input.c | tr ':' ' ' | sed -e 's/^typename //'
struct object * new_object (struct param * p);
int main (void);
$

This is similar to the answer posted by Steve Ward but this one requires sed, and tr instead of jq.


The tool cproto does what you want and allows to tune the output to your requirements.

Note: This tool also only works for C files.


I use ctags and jq

ctags --output-format=json --totals=no --extras=-F --fields=nP file1.c |
jq -sr 'sort_by(.line) | .[].pattern | ltrimstr("/^") | rtrimstr("$/") | . + ";"'

I use ctags

# p = function declaration, f = function definition
ctags -x --c-kinds=fp /usr/include/hal/libhal.h

Also works with C++

ctags -x --c++-kinds=pf --language-force=c++ /usr/include/c++/4.4.1/bits/deque.tcc

Note, you may need to add include paths, do this using the -I /path/to/includes.