Extracting C / C++ function prototypes

Peter picture Peter · Oct 15, 2009 · Viewed 20.5k times · Source

I want to do this:

extract_prototypes file1.c file2.cpp file3.c

and have whatever script/program print a nice list of function prototypes for all functions defined in the given C / C++ files. It must handle multi-line declarations nicely.

Is there a program that can do this job? The simpler the better.

EDIT: after trying to compile two C programs, bonus points for something that uses {perl, python, ruby}.

Answer

Johannes Schaub - litb picture Johannes Schaub - litb · Oct 15, 2009

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.