What order include header files?

I am new to programming, and the header file theme is similar to me when I started using many of them. In addition to this, I'm trying to use precompiled headers. I also use the SFML library, so I have those headers that should also be included.

Now I have stdafx.h, main.cpp, then the classes A, B, C and D contained inside Ah, A.cpp, Bh, B.cpp, Ch, C.cpp, Dh and D. castes.

What order will I include, including headers in all files, if

  • all classes contain an instance of the SFML class
  • class D contains an instance of class A and class C
  • class C contains an instance of class B My code: (note: all headers have header protection)

stdafx.h:

#include <SFML/Graphics.hpp> #include <iostream> 

hijras

 #include "stdafx.h" class A { //sfml class }; 

a.cpp

 #include "stdafx.h" #include "Ah" 

Bh

 #include "stdafx.h" class B { //sfml class }; 

B.cpp

 #include "stdafx.h" #include "Bh" 

Ch

 #include "Bh" class C: public B { }; 

C.cpp

 #include "stdafx.h" #include "Ch" 

Dh

 #include "Ah" #include "Ch" class D { A a; CC; // if left uncommented I recieve a '1 unresolved externals' error //sfml class } 

D.cpp

 #include "stdafx.h" #include "Dh" 

main.cpp

 #include "stdafx.h" #include "Dh" 
+7
source share
5 answers

My philosophy is that in well-written code, the header files should include all the other header files on which they depend. My reasoning is that you should not include the header file and get a compiler error for this. Therefore, each header file must (after #ifdef or #pragma once enable protection ) include all other headers on which it depends.

To informally check what you remember, to include the correct headers in the header files, * .cpp files must # include a minimal set of header files that should work. Therefore, if there are separate header files for A , B , C and D , and your cpp file uses class D , then it should include only Dh . Compiler errors should not occur because Dh #include Ah and Ch , Ch includes Bh and Ah and Bh include the SFML header (whatever it is). Ch and Dh may include the SFML header if this seems appropriate, but it is not necessary if you can be sure that the dependencies ( Bh and Ah ) have already included it.

The way Visual C ++ makes "precompiled headers" twists this logic. This requires including "StdAfx.h" as the very first header file, which forces many developers to simply put all #include for the entire project in StdAfx.h and not use #include in any of the other header files. I do not recommend this. Or they will place all external dependencies in StdAfx.h (for example, windows.h, formatting headers) and # include local dependencies elsewhere so that changing one header file does not necessarily lead to a rebuild of the entire project.

As I write my code, most of my CPP files include StdAfx.h and the corresponding .H file. So A.cpp includes StdAfx.h and Ah, B.cpp includes StdAfx.h and Bh, etc. The only other #include placed in the cpp file is the "internal" dependencies that do not appear in the header file. For example, if class A calls printf() , then A.cpp (not Ah ) will be #include <stdio.h> , because Ah is independent of stdio.h .

If you follow these rules, then the order in which you #include header does not matter (if you do not use precompiled headers: then the precompiled header will be the first in every cpp file, but you do not need to be included from the header files).

+9
source

Take a look at a similar question to explore a good approach to creating headings.

In short, you want to define each header inside the defender definition, which prevents the inclusion of headers more than once during compilation. For those in place, for each .h and .cpp file, just include the headers needed to allow any ads. The preprocessor and compiler take care of the rest.

+1
source

Ah must include SFML

A.cpp should include Ah

Dh must include SFML, Ah and Ch

D.cpp should include Dh

main.cpp should include any A, B, C, D, and SFML that it uses directly.

As a rule, in the .cpp file I do not include headers, which, as you know, should be included with its corresponding .h, because they contain definitions of the data members of the classes defined in this .h. Therefore, in my code D.cpp will not include Ah. However, I just would like to include it to remind you that the .cpp file (presumably) uses it.

This leaves stdafx - where you need it, it depends on what uses the things in it. It is probably needed everywhere, and MSVC does not process (or processes, but discards?) Anything before #include "stdafx.h" in the source file, so it should be the first in every .cpp file and does not appear anywhere else.

All header files must have several security devices.

You can add SFML (or anything else that you feel) to stdafx.h, in which case you could also remove them from other sources.

Once you have done this, it no longer matters in which order you include the headers in each file. Therefore, you can do what you like, but I recommend the Google C ++ style guide on this issue (click on the arrow), adjusted for stdafx.h.

+1
source

C inherits class B. So he should see identifier B So turn on Bh here -

 #include "Bh" // Newly added // Or you can forward declare class B ; class C: public B { }; 

D has objects of class A , B So, include the headings A, B in "Dh" itself.

 class D { A a; // Should see the definition of class A C c; // Should see the definition of class B //sfml class } 

D.cpp

 #include "Ah" #include "Ch" #include "Dh" // Notice that Ah and Ch should definitely placed before 

Please note that each header must be included in the corresponding source file. Think of each source file independently and see if it was previously installed or not in the source file.

+1
source

Depends on dependencies. Unlike C # and other similar languages, C ++ does things in the order in which they are written, so a problem may arise. If you have a problem with the order, it will not compile.

0
source

All Articles