This is a question about the c++ header files!

Asked 2 years ago, Updated 2 years ago, 22 views

I'm making a simple game, and someone said I need headers He made a comprehensive header file including etc.

Just pick up the header files you need and include them What are the advantages and disadvantages of both making and including a comprehensive header file like above??

c++

2022-09-21 21:45

1 Answers

The reason for creating a synthetic header file is that it is convenient.

When the compiler compiles the cpp file, it goes through a preprocessing process. At this time, #include is also processed in the preprocessing process, and all the contents of the header and the contents of the cpp are compiled as one. This is done for each cpp file that comprises the project.

If a.cpp and b.cpp include c.h, the contents of c.h will be compiled twice. Of course, the contents in c.h are mainly composed of function/variable/class declaration unless it is a template, so there will be no complicated work in compilation. However, no matter how main the declaration is, it will take a lot of time for the compiler to parse if the number increases.

In other words, an increase in the number of header files to include means an increase in compile time. Complete header file contains a cpp file that is compiled to include unnecessary headers so meaningless action is performed. This leads to a problem with compilation time.

Therefore, if you include only the header files that are essential for individual cpp files, you can reduce compilation time.

If you want to increase compile time due to headers added through #include, this is because the C++ build system has a method of adding header files through the preprocessor. This method is very outdated and unexpectedly burdens developers. To improve this, I understand that the latest C++ is preparing How to add a new module.

For your information, A precompiled header is sometimes created to solve the compilation time problem mentioned above.


2022-09-21 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.