Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A tool to auto separate C++ header and implementation

Tags:

c++

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.

like image 699
MajesticRa Avatar asked Feb 14 '12 11:02

MajesticRa


People also ask

How do you implement header files in C?

In C language, header files contain the set of predefined standard library functions. You request to use a header file in your program by including it with the C preprocessing directive “#include”. All the header file have a '. h' an extension.

What are the two ways of representing header files in C program?

There are of 2 types of header file: Pre-existing header files: Files which are already available in C/C++ compiler we just need to import them. User-defined header files: These files are defined by the user and can be imported using “#include”.

Which section of C language program contains all header files used in a program?

Every C program should necessarily contain the header file <stdio. h> which stands for standard input and output used to take input with the help of scanf() function and display the output using printf() function.

What are headers used for in C?

header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs. NOTE:Header files generally contain definitions of data types, function prototypes and C preprocessor commands.


2 Answers

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

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

like image 51
vulkanino Avatar answered Oct 01 '22 15:10

vulkanino


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.

like image 43
James Kanze Avatar answered Oct 01 '22 13:10

James Kanze