문제

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

도움이 되었습니까?

해결책

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

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

http://cfunctions.sourceforge.net

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

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

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.

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

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

You can run the source file through this program:

/* cproto_parser.c */
#include <stdio.h>                           
int main (void)                              
{                                            
    int c;                                   
    int infundef = 0;                        
    int nb = 0,                              
        np = 0;                              
    while((c=getc(stdin))!=EOF){             
        if(c=='{'){                          
            if((np==0)&&(nb==0)){infundef=1;}
            nb++;                            
        }                                    
        if (infundef==0) {putc(c,stdout);}   
        if(c=='}'){                          
            if((np==0)&&(nb==1)){infundef=0;}
            nb--;                            
        }                                    
        if(c=='('){np++;}                    
        if(c==')'){np--;}                    
    }                                        
    return 0;                                
}                    

Run through the preprocessor to get rid of comments. If you have unmatched braces due to #ifdefs you have to set defines, include files to make it not so.

e.g., cc cproto_parser.c -o cproto_parser; cc -E your_source_file.c|./cproto_parser

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top