31

I have a bunch of C++ header files with various class and function declarations. So far, when I've been writing the C++ source file implementations of the declared classes and functions, I've been manually:

  1. Copying the declarations from the header file to the corresponding source file.
  2. Deleting "class classname {" and the matching "};"
  3. Adding "classname::" onto the names of all the class functions.
  4. Replacing the semicolons after the function declarations with "{}".
  5. Deleting the keywords "virtual" and "static".

Only after all that work, which doesn't really do anything, can I actually go about implementing the functions. I am wondering if there is a tool out there somewhere that can automatically generate a ".cpp" file from a ".h" file, where the resulting ".cpp" contains empty stubs for the functions declared in the ".h" file (and for which an inline definition hasn't been given). A UNIX-friendly commandline tool would be preferred. Thanks.

UPDATE: A cross-platform tool would be ideal. If not, I am working on Mac OS X 10.6.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200

4 Answers4

24

Lazy C++ appears to be designed to address precisely that problem.

moonshadow
  • 86,889
  • 7
  • 82
  • 122
  • Lazy C++ does not appear to be installable on Mac OS X... which is what I am using. Are there any cross-platform generators that you know of that can be easily built from the source? – Michael Aaron Safyan Sep 10 '09 at 11:07
  • 1
    @Michael: You can grab the source and cross-compile for OSX, see e.g. answer to http://stackoverflow.com/questions/1375201/lazy-c-chicken-and-egg-problem – moonshadow Sep 10 '09 at 11:12
  • 1
    @moonshadow, I'm trying to save time, remember? It would probably take less time for me to write my own tool than to attempt to coax gcc into building as a cross-toolchain. – Michael Aaron Safyan Sep 10 '09 at 11:15
  • 1
    @Christian Adam, I have downloaded the source... unfortunately, it is self-bootstrapping. It requires a binary of Lazy C++ to build Lazy C++. – Michael Aaron Safyan Sep 10 '09 at 11:18
  • 4
    @Michael: Once you have the set of .h and .cpp files for 'lzz' the build is trivial, you'd just copy the source to your mac and then build it there. The code is high quality C++. It always makes me laugh when I hear someone say: "it would probably take less time for me to write my own...". I would be interested in any examples you have from your past where that has turned out to be true! – Richard Corden Sep 16 '09 at 10:48
  • @Richard, true enough. I wasn't really serious about writing my own... that is why I asked the original question in the first place. – Michael Aaron Safyan Nov 08 '09 at 02:25
7

I found myself in your situation lately and manned up to write my own tool -- impl_me. It's a small Ruby script that uses SWIG as a parser backend. It writes to stdout so you can combine with your favorite nix toolset find/grep/sed/awk to tweak for your own preferences.

Because it's written in Ruby, it should be cross platform. SWIG is also cross platform so everything should be OK.

It's quite primitive at this stage and isn't as robust as Lazy C++ in terms of parsing strange templates and stuffs. Contributions are welcome :)

kizzx2
  • 18,775
  • 14
  • 76
  • 83
  • It seems that the swig tool is not working correctly anymore. I'm getting errors about the gem being renamed to optimist. – HSchmale Feb 23 '19 at 21:56
4

The eclipse CDT has an "Implement method" feature which does just that (one method at a time). There is also a "Generate Getters and Setters" feature which also generates the appropriate code in the function bodies.

Jonas Bötel
  • 4,452
  • 1
  • 19
  • 28
  • 1
    Thanks. I'm looking for a commandline invocation, though... something that I can use in a "find" command to recursively generate ".cpp" files for all the header files in my project. – Michael Aaron Safyan Sep 10 '09 at 21:48
1

I tried Lazy C++ but it seems to be out of date and not maintained anymore. It doesn't support the latest C++14 standard either.

That's why I decided to write my own tool in Java using ANTLR4. It's called Score and you can find it here: https://github.com/underrated/Score

At the moment it's in its infancy and might be full of bugs but I plan to improve it. So give it a try if you want and report any bugs on the project's github page. I'll try to fix them as fast as possible.

  • I don't expect it to handle preprocessor conditionals at the moment but I plan to add support for that. Other than that, the tool is not intended to do semantic checks on the code. It just identifies the method implementations and then it puts the method prototypes in the header and the method implementation in the source file(plus a few more "separations"). It's up to the developer to insure the correctness of the code(e.g. by using an IDE). If the original code has syntactic/semantic errors then so will the generated one. But they can be found, either by the IDE or by the compiler. – Tiberius P. Aug 09 '16 at 09:35
  • @IraBaxter Why would you need such a complex handling of things in the context of Michael's problem? All he needs is to automate the copying/pasting/modifying of some code. Special treatment for #ifdefs is indeed necessary. Templates don't need to be touched, they can stay in the header. Macro calls will be seen as simple identifiers or function calls by the parser and don't need to be touched either. Regarding namespaces, all the "using ..." statements can stay in the header. And the "namespace ... { ... }" can be copied to both the header and the source file. Am I missing something? – Tiberius P. Aug 09 '16 at 13:51