5

I was wondering if there was a tool (maybe GNU C++ compiler) to obtain from a .cpp file full of functions a .hpp file with the mentioned functions declarations.

Example:

I have:
magic.cpp

int foo() { return 42; }
char bar() { return 'z'; }

And I would like to obtain this after applying the wonderful tool:
magic.hpp

int foo();
char bar();
Pubby
  • 51,882
  • 13
  • 139
  • 180
msemelman
  • 2,877
  • 1
  • 21
  • 19
  • possible duplicate of [Seeking code stub generator (from header files)](http://stackoverflow.com/questions/2020568/seeking-code-stub-generator-from-header-files) –  Dec 22 '11 at 23:39
  • Search and replace `{` with `;/*` and `}` with `*/` will do it, although not a good solution. – Pubby Dec 22 '11 at 23:40
  • @Tim: That's the other way around, non? – Xeo Dec 22 '11 at 23:41
  • @TIM Yes, Xeo is right, what you show us is the other way around, i already have the stubs. – msemelman Dec 22 '11 at 23:54
  • @PubbY MMmmhhh, i was looking something more automated. What if i have definitions or includes? – msemelman Dec 22 '11 at 23:55

3 Answers3

4

On Debian-based distributions:

apt-get install cproto

Then cproto magic.cpp gives the following output:

/* magic.cpp */
int foo(void);
char bar(void);
Eduardo Ivanec
  • 11,668
  • 2
  • 39
  • 42
3

Please see Dehydra or Treehydra. One of these two tools should allow you perform this via GCC.

Dehydra is a lightweight, scriptable, general purpose static analysis tool capable of application-specific analyses of C++ code.

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
2

To do this accurately, you need a full C++ parser and name resolver because you need not only the function declarations but also the context that makes them well defined. For instance, if you have a declaration

 void foo<T>(...){ ... }

in the file, and there's a type T in the file, you better retain the declaration for T, and whatever declarations are used to define T, etc. So you have keep the web of definitions supporting foo... and you have to decide if you want to keep those that come from include files or not.

Our DMS Software Reengineering Toolkit is customizable general purpose program analysis and transformation machinery. With its C++ Front End, one can parse C++ code (including #includes), build Abstract Syntax Trees (ASTs), resolve names and types out of the box. Customization code would then determine the web of references necessary, and for each reference, convert it into a signature (instead of an implementation) if necessary, by applying source-to-source program transformations to the ASTs. DMS's prettyprinter could then produce the final output text.

DMS has been used for large scale C++ (re)engineering on other big projects.

Of course, you can always write some bad hack to simulate this, if you don't mind an answer you are likely to have to patch by hand.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • Great answer! I was wondering it was not easy. But i would never thought it wasn't solved in the open source world. Right now, what I am writing is code for a course project for my university, so what you are offering me is out of range. However it seems the right tool. I'll wait for a more general-public-friendly solution, but if that never happens i think this is the best solution available. – msemelman Dec 23 '11 at 00:03