Including files in header file and implementation file (C ++)

What is the difference between including a header file in a header file and including it in an implementation file?

it

Example:

// test.h #include"globals.h" class Test { Test(); }; 

vs

 //test.cpp #include"globals.h" Test::Test() { } 
+6
source share
7 answers

The general principle is that you want to minimize dependencies wherever possible, like this:

  • if your interface (.h) refers to anything in this header, then that header should be # included in the interface (.h)

  • if you only reference this header in your implementation (.cpp) (and not your interface), you should only # include this header in the implementation

  • you should also only try # to include headers that are really needed, although it can be difficult to maintain a large project throughout life

So, for your example above, if you are not referencing anything from globals.h in test.h, but you are referencing it in test.cpp, then #include should go to test.cpp. If you are referencing something from globals.h in test.h, then you will need #include in test.h.

+3
source

If you include some implementation-specific external headers, you are better off including them in a cpp file to reduce the dependence of the header on the API file. Including third-party headers in cpp files is a good way to hide data so library users do not know about your links.

It’s good practice to include header files exactly where they are needed in order to make your code more consistent and make your project easily modifiable for future development.

+1
source

No difference. .h files are used to define classes and variables, while .cpp is an implementation.

We use:

  • #include <> when the lib / h file is available in the lib folder.
  • #include "" when it is in the current folder .. (lib path not specified)
0
source

The only thing compiled is the .cpp file.

Actually, this is not so; the file generated from .cpp is compiled.

When you create this other file, the #include directives efficiently copy and paste the contents of the included file into the generated file.

That is all that happens.

0
source

As far as I know, there is no difference in including the header file in the source or header file. Please note that #include is a preprocessor macro, and all it does is that it replaces the contents of the header file in the place where it is included.

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

 #ifndef GLOBALS_H_ #define GLOBALS_H_ #define MYGLOBAL_VARIABLE 10 #endif /* GLOBALS_H_ */ 

source files after the preprocessor completes will look like this.

 /* #include "globals.h" */ #ifndef GLOBALS_H_ #define GLOBALS_H_ #define MYGLOBAL_VARIABLE 10 #endif /* GLOBALS_H_ */ Test::Test() { } 
0
source

#include is a simple text replacement. The string is replaced by the contents of the specified file. So, if you include Ah in Bh and Bh in C.cpp, then the contents of Ah will eventually be inserted in C.cpp.

Usually we try to avoid such headings in headings. Often forward declarations can be used instead. For instance. class Global; . The big exception is for base classes. The title that defines the derived class must include the title for the base class.

0
source

If you provide your code as an API in the form of headers and libraries, then you must make sure that there are no internal private header files inside your header files that define the interface. In this case, you include all your personal files in the CPP file, which will be compiled and will be in the .lib or .a or .dylib files. Otherwise, it will be a problem for the user who uses your API.

You need to make sure that the files you add to your header say that globals.h is available in the place where you included test.h. If not, include globals.h in the CPP file.

0
source

All Articles