16

I always feel a pain when I switch from C# or python back to C++ and meet the .h and .cpp separation.

So I thought that maybe there is a tool that at pre-compilation step can take header (o file with some special extension) and split it to .h and .cpp?

So if original file like this:

class MyClass
{
public:

    void HaHaHa()
    {
       //some logic
    }
 }

And a result would be as .h and .cpp files:

//.h 

class MyClass
{
public:

    void HaHaHa();
}

// .cpp 

#include "MyClass.h"

void MyClass::HaHaHa() 
{
    //some logic
}

Some googling didn't show up the ready to use tools. But I'm pretty sure it is not a new idea and such tools should exist.

P.S. It is known that i.e. Visual Assist X and VIM has tools to handle .h and .cpp separation with less pain. But I'm asking about a possibility to have a code in one files and separate them automatically as a part of build process.

MajesticRa
  • 13,770
  • 12
  • 63
  • 77
  • 3
    And why not then put all code in the h files? Slow compiles? – Prof. Falken Feb 14 '12 at 12:01
  • Modules will sooner (or later) come to c++, see: http://stackoverflow.com/questions/3596147/modules-in-c0x this will solve it hopefully. – tauran Feb 14 '12 at 12:06
  • 2
    "Slow compiles" - as example. Slow parsers of autocomplition is even worse. Or sometimes one write some library and don't want it to be "open source". In general, C++ is designed to have this separation. In this point of view having such tool is much clear solution. – MajesticRa Feb 14 '12 at 12:07
  • @tauran I wish it to come sooner! But unfortunately today we live in "today" and not in "tomorrow" – MajesticRa Feb 14 '12 at 12:10
  • Does this answer your question? [Automatically split (refactor) .h into header and implementation (h+cpp)](https://stackoverflow.com/questions/11184366/automatically-split-refactor-h-into-header-and-implementation-hcpp) – Ciro Santilli OurBigBook.com May 13 '20 at 17:37
  • Does this answer your question? [Automatically generate C++ file from header?](https://stackoverflow.com/questions/1404614/automatically-generate-c-file-from-header) – fdermishin Dec 22 '20 at 09:46

3 Answers3

8

This tool may help you: lazycplusplus [Wayback link because the domain was lost]

Project repo: https://github.com/mjspncr/lzz3

Carl Walsh
  • 6,100
  • 2
  • 46
  • 50
vulkanino
  • 9,074
  • 7
  • 44
  • 71
  • Thank you! That looks really what I was asking! Now it is time to check it in action... – MajesticRa Feb 14 '12 at 12:11
  • 2
    Given that the parser has been hand-made, I am skeptical about its ability to parse real C++ statements: templates, function types, ... – Matthieu M. Feb 14 '12 at 12:13
2

I think you're going about it backwards: you want to write the header file, without the implementations, before writing the code. What would be nice is a tool which would read the header file and generate the outline code for the implementation: with namespaces, nested classes and such, just the wrappers can be quite verbose. But the closest I've seen is Rational Rose, which starts with a (highly) annotated ULM diagram, and generates both the header and the boilerplate of the implementation. It's a very nice tool—I use it whenever it's available—but it's a bit pricy for the home user, and probably even for small corporations.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

Under Windows Visual Assist X by Whole Tomato does that sort of thing. Not automated though I don't think.

PowerApp101
  • 1,798
  • 1
  • 18
  • 25