Rajeshwar
Rajeshwar

Reputation: 11651

Management of lot of #include statements and header files

In my current project. A lot of my .cpp and .h files have plenty of includes in them such as 6 or 7 of headers declared in the following manner.

#ifndef A_Header
#include "a.h"
#endif

#ifndef B_Header
#include "b.h"
#endif

I wanted to know would it make sense if I wrapped all of these headers (used in the project) in a single header and then declare that header in each source file as such

#ifndef Wrapper_Header 
#include "wrapper.h" /*This would contain a collection of all headers*/
#endif

Any suggestions and drawbacks of this plan that I am not anticipating?

Upvotes: 0

Views: 155

Answers (3)

Ed Heal
Ed Heal

Reputation: 59997

Potatoswatter is correct

But I like to add use "forward declaration".

I am sure you can google it.

Upvotes: 0

Potatoswatter
Potatoswatter

Reputation: 137800

That's totally bizarre.

Every header should contain a header guard:

#ifndef THIS_HEADER
#define THIS_HEADER

/* contents of the header */

#endif

This goes inside the header, not in the including .cpp file. The compiler detects the header guard and avoids re-reading all the text when it's included again. This can save seconds of compilation time.

If your headers have that, then the guards in the .cpp file are extraneous and you should remove them. 6 or 7 headers isn't a lot, but that silly boilerplate does sure add up.

Upvotes: 2

M. Utku ALTINKAYA
M. Utku ALTINKAYA

Reputation: 2272

Never wrap your headers, always be explicit, make it clear what is included, and do not include what you do not need.

Upvotes: 0

Related Questions