Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate .cpp source from a .h file

The barebones version of a .cpp file is often not much more than a copy of the related header file with some braces instead of semicolons, ClassName:: inserted in a few places, and removing the virtual keyword. Much of it could be generated with a one-line sed command, but it wouldn't be very robust for all the language features (templates come to mind).

Before I go write my own, are there any simple command-line utilities that will take a header file I provide and give me a skeletal .cpp file where I can fill in the blanks?

like image 428
Dave Ceddia Avatar asked Oct 25 '10 19:10

Dave Ceddia


People also ask

Does every .h file need a .cpp file?

Cpp files don't always have to have a header file associated with it but it usually does as the header file acts like a bridge between cpp files so each cpp file can use code from another cpp file. One thing that should be strongly enforced is the no use of code within a header file!

Is .h file a source file?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

What is the difference between a .h and a .cpp file?

The short answer is that a . h file contains shared declarations, a . cpp file contains definitions and local declarations. It's important that you understand the difference between declarations and definitions.


2 Answers

Most graphical editors have some plugins available to do that like vim, gedit or Eclipse. But I am not aware of a shell or perl script to do that.

like image 91
Benoit Thiery Avatar answered Oct 10 '22 07:10

Benoit Thiery


Eclipse CDT has a feature of creating setters/getters provided you have defined variables in your .h. Also it provides "Implement method" - again it looks through your definitions in .h and implements methods giving them empty body. Maybe someone find this usefull.

And don't forget to read question, pointed by larsmans.

like image 42
PF4Public Avatar answered Oct 10 '22 06:10

PF4Public