CodingMadeEasy
CodingMadeEasy

Reputation: 2337

Including files in a header vs implementation file

What's the difference between including a header file in a header file vs including it in a implementation file?

This

Ex:

// test.h
#include"globals.h"

class Test
{
    Test();
};

vs

//test.cpp
#include"globals.h"

Test::Test()
{
}

Upvotes: 9

Views: 5673

Answers (7)

Frankenstein
Frankenstein

Reputation: 430

No difference as it is. .h files are used to define classes and variables whereas .cpp files are the implementation.

We use:

  • #include <> when the lib/h file is available in the lib folder.
  • #include "" when it's present in the current folder.. (lib path not given)

Upvotes: 0

Bharath
Bharath

Reputation: 85

As far as I know, there's isn't any difference in including a header file in the source or the header file. Note that #include is a preprocessor macro and all it does is that it replaces the contents of a header file at the location where it is included.

In your example above, if the globals.h looks like this,

#ifndef GLOBALS_H_
#define GLOBALS_H_

#define MYGLOBAL_VARIABLE 10

#endif /* GLOBALS_H_ */

the source files after preprocessor finishes its job would look like this.

/* #include "globals.h" */
#ifndef GLOBALS_H_
#define GLOBALS_H_

#define MYGLOBAL_VARIABLE 10

#endif /* GLOBALS_H_ */

Test::Test()
{
}

Upvotes: 0

tejusadiga2004
tejusadiga2004

Reputation: 311

If you are providing your code as API in the form of Headers and Library, then in you must make sure that there is no internal private header files included in your header files that defines the interface. In this case you include all your private files in CPP file which will be compiled and will be in .lib or .a or .dylib file. Else that would be a problem to the user who uses your API.

You should make it sure that the files you include in your header say globals.h is accessable at the place where you include test.h. If this is not the case then include globals.h in CPP file.

Upvotes: 0

MSalters
MSalters

Reputation: 179779

#include is a simple textual replacement. The line is replaced by the contents of the mentioned file. So, if you include A.h in B.h, and B.h in C.cpp, then the contents of A.h eventually will be pasted into C.cpp.

We usually try to avoid such headers-in-headers. Often it's possible to use forward declarations instead. E.g. class Global;. The big exception is for base classes. The header which defines the derived class must include the header for the base class.

Upvotes: 0

fatihk
fatihk

Reputation: 7919

If you are including some implementation specific external headers, you had better include them in cpp file to reduce header dependeny on the API file. Including third party headers in the cpp files is a good way for data hiding so that library user will not know much about your references.

It is a good practice to include headers files in just where they are needed to increase the eligibility of your code and making your project easily modifiable for future development.

Upvotes: 2

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

The only thing compiled is the .cpp file.

Actually, that's not true, a file generated from the .cpp is compiled.

When generating this other file, #include directives effectively copy and paste the contents of the include file into the generated file.

That's all that's happening.

Upvotes: 0

Paul R
Paul R

Reputation: 212929

The general principle is that you want to minimise dependencies wherever reasonably possible, so:

  • if your interface (.h) references anything in a given header then that header needs to be #included in the interface (.h)

  • if you only reference a given header in your implementation (.cpp) (and not in your interface) then you should only #include that header in the implementation

  • you should also try to only #include headers that are actually needed, although this can be difficult to maintain during the lifetime a large project

So for your example above, if you don't reference anything from globals.h in test.h, but you do reference it in test.cpp, then the #include should go in test.cpp. If you reference anything from globals.h in test.h though then you need the #include in test.h.

Upvotes: 8

Related Questions