3

this sounds weird, but maybe there is exist one... i googled for it, but didn't find anything.
simple example:
i have one file class1.h:

#include "a.h"
#include "b.h"

another file class2.h:

#include "a.h"
#include "c.h"

and main.cpp:

#include "class2.h" //as we see here, we getting "a.h" double included by class1.h and class2.h

i want to get rid of such dupes in my project.

sure, in example it not that hard, but i have huge amount of files which includes each other in many ways and hard to trace all dupes by myself.

any suggestions, before i going to code that tool by myself? :)

Kosmo零
  • 4,001
  • 9
  • 45
  • 88
  • 5
    Simple solution: include guards. At that point it doesn't matter nearly as much whether something gets included twice, as subsequent includes will be ignored. – cHao Mar 17 '12 at 07:44
  • that's sounds really simple. thanks – Kosmo零 Mar 17 '12 at 07:51
  • Four answers and no one actually answered the question... – jww May 05 '17 at 00:36
  • Possible duplicate of [Tools to find included headers which are unused?](https://stackoverflow.com/questions/1301850/tools-to-find-included-headers-which-are-unused) – phuclv Nov 28 '17 at 08:39
  • [C/C++: Detecting superfluous #includes?](https://stackoverflow.com/q/614794/995714) – phuclv Nov 28 '17 at 08:40

4 Answers4

6

As i see it, if the code in a file relies on other stuff to be declared, it should include the file that contains those declarations. Otherwise, stuff gets brittle and includes have to be done all the time and in a certain order, and it all gets ugly. So to me, the "duplicate includes" are a good thing; each piece of the code can take care of its own dependencies with less hassle.

As for how to keep things from breaking on duplicate includes...there's an idiom called "include guards". They look a little something like this...

(file1.h)

#ifndef FILE1_H
#define FILE1_H

(the meat of file1.h goes in here)

#endif

When this file is included the first time, FILE1_H isn't defined yet (or shouldn't be, assuming you pick better names for your files than this), so the code gets included (and FILE1_H subsequently gets defined). The next time it's included, though, FILE1_H is now defined, so the code gets skipped.

(By the way, any macro name will work for this, as long as it's unique per file. But that particular constraint is the biggest reason the macro's name is almost always based on the file name.)

On most compilers, #pragma once works as well. But it's not standard; if you care about being able to use any compiler you want, then don't use it (or use it in addition to the include guard).

cHao
  • 84,970
  • 20
  • 145
  • 172
  • 1
    Some compilers even recognize include guards and don't open the same header file again, to save time. –  Mar 17 '12 at 08:13
2

You should use Include Guards so that You do not include the same header multipe times in the same translation unit.

Once You have the Include guards in place You don't have to bother about including header files multiple times. A good practice is that each source or header file should include header files it needs for independent compilation, ie: should not rely on includes through other included files.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

In addition what @Als said, start using forward declarations.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • +1 for suggesting forward declarations. In such scenarios, these will be the #1 optimization for speeding up compile time. – ComicSansMS Mar 17 '12 at 07:53
0

I have used #pragma once extensively with my c++ projects, but as I have heard somewhere, it may not work for all compilers, but if you are using Microsoft Visual Studio, this works (I have used it in VC++ 6.0, 2005 and 2010 )

Yash Gupta
  • 578
  • 6
  • 15
  • 1
    I believe {soft|hard} links on Unix and Linux can cause problems for `#pragma once`, IIRC. I thought I read an article or blog by Stallman about it, but I cannot find it at the moment. Also see [Is #pragma once a safe include guard?](http://stackoverflow.com/q/787533/608639). – jww May 05 '17 at 00:39