views:

524

answers:

7

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}.

+1  A: 

http://cfunctions.sourceforge.net

(This only does C and a limited subset of C++. Disclaimer: this is my program.)

Ben Bullock
hmm, it doesn't compile.
Peter
Thanks for letting me know.
Ben Bullock
here's a slightly more useful piece of information, in case it helps... http://pastie.org/655762
Peter
Ben Bullock
The new version seems to be showing up on sourceforge now. If you have the time and patience left to try it out again, please let me know if you find any more problems. Google group is http://groups.google.com/group/cfunctions
Ben Bullock
still couldn't get it working much at all with c++, unfortunately.
Peter
+1  A: 

I used to use doxygen to generate documentation for my C++ code. I am not an expert, but i think you can use doxygen to generate some sort of index file of the function prototypes.

Here is a thread of someone asking a similar question

Andrew Keith
Gah, beaten to it. I agree though :-)
Jon Cage
+2  A: 

If you format your comments suitably, you could try DOxygen. In fact, if you've not tried it before I'd recommend giving it a go anyway - it will produce inheritance graphs as well as full member function lists and descriptions (from your comments).

Jon Cage
yeah, been down the doxygen path and I'm after something much simpler for this one task. (I'll leave the discussion about the merits of doxygen to another thread.)
Peter
You don't actually have to comment anything. Just make sure your Doxyfile has the option for generating documentation for all members turned on. But agreed, this is quite complicated work, although IIRC there's an XML output option that you could try parsing.
blwy10
+4  A: 

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.

Frank Bollack
argh, having trouble compiling stuff tonight it seems (on mac). looks good though...
Peter
A: 

gcc-xml might help, although as it is, it only does half the job you want. You'll need some processing of the XML output

sbk
A: 

gccxml is interesting, but it print a xml tree. You need to extract information about class, functions, types, and even the specialized templates of class and functions. gccxml use parser of GCC, so you don't need to do the worst job wich is parsing C++ file, and you are 100% sure that it's what probably the best compilator understand.

lionbest
+2  A: 

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
Johannes Schaub - litb