Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting C / C++ function prototypes

Tags:

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

like image 416
Peter Avatar asked Oct 15 '09 08:10

Peter


People also ask

What is C function prototypes?

A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.

Does C require function prototypes?

It is not required, but it is bad practice not to use prototypes. With prototypes, the compiler can verify you are calling the function correctly (using the right number and type of parameters).

What are C++ prototypes?

A function prototype is a declaration in C and C++ of a function, its name, parameters and return type before its actual declaration. This enables the compiler to perform more robust type checking.

What is Cproto?

Description. cproto generates function prototypes for functions defined in the specified C source files to the standard output. The function definitions may be in K&R or ANSI C style, or in lint-library form. cproto can also convert function definitions in the specified files from the K&R style to the ANSI C style.


2 Answers

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.

like image 157
Johannes Schaub - litb Avatar answered Sep 22 '22 09:09

Johannes Schaub - litb


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.

like image 24
Frank Bollack Avatar answered Sep 20 '22 09:09

Frank Bollack