3
votes

I have a few queries regarding the design principle of laying out a C++ header and source files: I have recently taken over a project in which the previous programmer used to have this, which is particularly annoying because I read somewhere that we shouldn't include a .cpp file in a .hpp file (The preprocessor will just copies and pastes the .cpp file into a .hpp)

Q1. Including a .cpp file in a .hpp file is bad? why?


Due to the problem above, I am facing many "multiple declaration" errors when I load my program in eclipse, even though i added the header guards in all the .hpp files.

Q2. Should i be including the header guards in the .cpp files as well? I tried the later too but to no avail. Any suggestions on this?

Q3. If 2 or more of my .cpp files need the same header files to be used what is the best way to include all those header files? Should i create a new header file say h1.hpp, include all the header files I need in those 2 or more .cpp files and later include in this header file in those .cpp files(s)?

Is it an efficient approach ?

1
Hope the madness with h/cpp files will be gone soon in the new C++ standard.Andrey Nasonov
@AndreyNasonov That made me curious, how?jnbrq -Canberk Sönmez
Header guards do not help against multiple definitions because header guards allow multiple inclusions of the guarded file, just not by the same translation unit. You need to separate declarations and definitions. Put declarations into the header file, definitions into the cpp file and everything will work fine.nwp
@nwp All of my declarations are in .hpp file and the definitions are in .cpp files. I am also using class template BTW, wondering if those are messing stuff up.SeasonalShot
@jnbrq-CanberkSönmez modules.Bartek Banachewicz

1 Answers

5
votes

Including a .cpp file in a .hpp file is bad? why?

In a typical code setup, yes. It serves no useful purpose and can lead to "duplicate definition" errors.

More importantly, it mixes the separation between the implementation and interface parts. When a file containing implementation is meant to be included, it's often changed to .inl (from "inline") extension.

Should i be including the header guards in the .cpp files as well?

No. Header guards prevent two (or more) other headers in one translation unit from including the same header twice. Since there's only one .cpp file per translation unit, this problem doesn't occur there.

To illustrate, an example inclusion could look like this:

  common.hpp
   /      \
  /        \
A.hpp     B.hpp
  \        /
   \      /
   file.cpp

In this case, header guard in common.hpp prevents it from appearing twice in the TU introduced for file.cpp.

If 2 or more of my .cpp files need the same header files to be used what is the best way to include all those header files?

You shouldn't be scared by a long include chain, in general. It's less scary than it looks. That being said, "aggregate" headers can be used if the headers actually form a tree structure (to make including subsets easier, like collections.hpp and collections/vector.hpp + collections/list.hpp) or to include every header from the library.