Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Master Include Files - Good or Bad Practice

Tags:

c++

c

I have seen quite a few projects (often game engines) where all the header includes are placed in a single header file which sometimes contains macros etc as well e.g.

// Master.h

#include "header1.h"
#include "header2.h"
#include "header3.h"
.
.
#include "headerN.h"

Then when using code, the standard would be to only include Master.h file.

Other projects work on the basis that the source files should include only the headers they need.

What I want to know is if there is a definitive answer as to best practice, preferably with measurable results, or is it personal preference?

like image 569
Zammalad Avatar asked Mar 27 '13 16:03

Zammalad


People also ask

Is it bad practice to include .cpp files?

Yes, it's bad practice. It's also bad practice to include it in the corresponding header.

Should you write functions in header files?

Unless you want the function to be inline , it is best to declare the function in the header and define it in a single source file and link it. If you declare the function as inline , then each of its function call in the source file will be replaced with the code inside the inline d function.

What is IWYU?

In a nutshell, include what you use (iwyu), introduced by Google, ensures source files include all headers used in the C++ code. This c++ include what you use methodology essentially maximizes the probability that code continues to compile even with reasonable changes made to the various interfaces.

What is the method to include header file Mcq?

To include a user defined header file one should use #include”name. h” i.e. enclosed within double quotes.


1 Answers

Most of the answers are mentioning compile time, and ignoring the fact that precompilation of headers has huge compile time benefits, and works much better with the master header technique.

Preferably, your headers should work if directly included without a master header file (this makes testing easier). Modern compilers have optimized the "skip header file if multiply included" logic.

like image 79
Ben Voigt Avatar answered Sep 30 '22 16:09

Ben Voigt