Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cannot modern C++ IDEs auto-generate header files?

Tags:

I understand the benefits and flexibility that header files provide, symbol discovery, the speed-up of processing for the compiler, etc. What I don't understand is why modern C++ IDE's don't auto-generate header files based on the members/methods added into the code file, thus reducing the manual labour involved in keeping the header up-to-date with the code file, and vice-versa. Since the IDE only makes incremental changes to the header file, developers may modify the header and changes are preserved.

Refactoring could be provided for adding/renaming/removing method arguments, for renaming methods, for moving methods into another class, and so on. During such refactoring the IDE would take care of updating the header + source files.

The functionality could be similar to the Visual Form Designer in Visual Studio. When you design a form, the IDE auto-generates code for the same, which is stored in a separate, IDE-managed source file. Devs may also modify such code files, or may include additional code in the user-managed source file.

Working with professional C++ source code, I've encountered all kinds of dubious practices :

  • Entire classes defined in the header file, that include function code ("why should I define a class in two places when I can define it in one?!")

  • Useful functions defined in the header file ("why bother keeping the header up-to-date, when I can define the function in the header itself?! let other devs use "go to declaration" to find the function if they don't think of looking in the header file!")

  • Missing header definitions for public/static functions (reduces compilation time?, or saves dev. time?)

Although I am not a professional C++ programmer, coming from a high-level background (JS, C#, AS3), I can feel the "downgrade" working with C++ first-hand and I don't see why some of these disadvantages cannot be eliminated by the IDE itself.

By no means am I mocking the IDE or the compiler in any way. I understand C++ allows for more complex methods of defining a program than modern-day languages do (eg. C#), and though the complexities of templating elude me, I'd like to see some of the benefits of higher level languages brought into C++ application development.

like image 359
Robin Rodricks Avatar asked Dec 13 '14 05:12

Robin Rodricks


People also ask

Why can't we compile template code into its own .O file?

Because the templates are not functions, they can't be compiled separately.

How do I add a header file in Makefile?

The only way to include the header file is to treat the filename in the same way you treat a string. Makefiles are a UNIX thing, not a programming language thing Makefiles contain UNIX commands and will run them in a specified sequence.

Can we create header file in C?

So the question arises, is it possible to create your own header file? The answer to the above is yes. 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.

Does C Sharp have header files?

Microsoft created C# to remove complexities from C++ so they removed header files.


1 Answers

The premise of your question, I believe, is not correct.

The Eclipse Luna IDE can auto-generate implementation stubs (in a CPP file) after you define a function (or class member function) in a header file.

You type the following in the header:

class MyClass 
{
  void my_method(const OtherClass& o, const std::string& name) const;
};

Then click "Source > Implement Method". Eclipse correctly generates something like this in a CPP file:

void MyClass::my_method(const OtherClass& o, const std::string& name) const
{
   // TODO: Auto-generated method stub.
}

I think this is a more typical use case than "[adding] members/methods... into the code file" and then "auto-generat[ing] header files". The idea is that most developers will design (create) an interface, and then develop an implementation (sometimes in a CPP file).

As for "Useful functions defined in the header file", this is common practice where developers want their functions to be inlined by the compiler in many translation units.

like image 177
NicholasM Avatar answered Nov 15 '22 12:11

NicholasM