The role of the header file stdafx.h (common mistakes), and how to add this header file in an empty project


Precompiled header file stdafx.h

stdafx.h is a default file in Microsoft's own pre-compilation mechanism, which fills in the h file used in the code, and when VS compiles the code, it will pre-compile the h mentioned in stdafx.h and cache it. When the user does not change the contents of stdafx.h, these h will not be recompiled, which will allow you to perform repeated run/debug operations more efficiently.
When you set the vs project to [do not use the pre-compilation mechanism], it may cause the compilation speed to slow down every time you debug and run.

The most common usage scenario is the stdafx.h file, which contains commonly used system header files, such as windows.h, cstdio, string, because this header file will not be changed (modified) frequently, and will be used by many .cpp files , so that you only need to let other .cpp files include the stdafx.h header file; if you change the stdafx.h file at will, the whole program will be recompiled .
When compiling, although stdafx.h is included by multiple .cpp, it will only be compiled once, which speeds up the compilation speed.
stdafx.cpp is specially used to generate precompiled files. There is only one line of code #include “stdafx.h” in stdafx.cpp, compiling stdafx.cpp will generate a .pch file. This .pch file will be used when other .cpp files are compiled.

If you want to cancel the #include "stdafx.h" automatically generated by VS2012, just change the option to: [Do not use precompiled headers].


fatal error C1083: cannot open include file: "stdafx.h": No such file or directory

Sometimes our .cpp file and the stdafx.h header file are not in the same level of directory, we only need to change the path of #include "stdafx.h" to a relative path .
For example, change it to #include "...\stdafx.h", because stdafx.h is in the upper-level directory of the .cpp file;
if it is in the upper-level directory of the .cpp file, change it to #include " ...\stdafx.h" is enough.


fatal error C1083: Unable to open precompiled header file: "Debug\Win32RegistryClass.pch": No such file or directory

insert image description here
This error is caused when the precompiled header of my .cpp file is set to "use (/Yu)", when I set the precompiled header of any .cpp in the above picture to "create (/Yc)" , the compilation is passed.


warning C4627: “#include “…\stdafx.h””: skipped when looking for precompiled header usage

Solution:
Open the .cpp file with compilation errors, and add #include "stdafx.h" to the first line (must be the first line).


fatal error C1010: Unexpected end of file encountered while looking for precompiled header. Did you forget to add "#include "stdafx.h"" to your sources?

This error occurs because when the compiler is looking for a precompile directive header file (default #include "stdafx.h"), the file does not end as expected. The header file "stdafx.h" for the precompile directive was not found.
Because each cpp file attribute in the project uses the precompiled header (/YU) by default (if you choose to use it, you must explicitly write #include in the source file), but the added third-party file does not have #include "stdafx.h" precompiles the header, so the compiler doesn't find it all the way to the end of this cpp file.
Reference link:
fatal error C1010: Unexpected end of file encountered while looking for precompiled header.

When we obviously have stdafx.h and stdafx.cpp in our project, we also added this header file at the beginning (first line) of the .cpp file, and we can also open this header file (indicating that the path of stdafx. However, when adding the path of this header file, we use a relative path, for example #include “…\stdafx.h”.

Solution 1:
Either you right-click the .cpp file, Properties --> All Configurations --> C/C++ --> Precompiled Header --> Select: Compile without using the precompiled header.
Solution 2:
Modify #include "…\stdafx.h" to #include "stdafx.h", as shown in the following figure:

insert image description here
Although the IDE intelligently prompts that there is a problem, the compilation can pass without affecting the use (but you cannot right-click the stdafx.h in the above picture to open this document, after all, the path is wrong, indicating that the .cpp file does not need this stdafx.h Header files, we can set the .cpp not to use precompiled headers).


Add stdafx.h in empty project

When we build a win32 project, there is no such header file when we create an empty project, but if we directly follow the steps of building a project, this header file stdafx.h will be automatically generated.
Then when our project is built from an empty project , if an error is reported when compiling: warning C4627: “#include “ServerDlg.h””: skipped when looking for precompiled header usage, or even more serious errors: fatal error C1010 : Unexpected end-of-file encountered while looking for precompiled headers. Did you forget to add "#include "stdafx.h"" to the source, what should we do when we want to add this "stdafx.h"?

Solution:
If the cpp file cannot find stdafx.h in the directory where it is located during compilation due to various reasons, then
1. Use Notepad to create a new blank file and place it in the folder where the cpp that references stdafx.h is located.
2. Rename the newly created txt file to stdafx.h, and the content can be empty.
3. Right click on the .cpp file --> Properties --> All Configurations --> C/C++ --> Precompiled Header --> Select: Use Precompiled Header (/Yu) 4. Recompile the code
.


How to use precompiled headers

  1. Create a precompiled header file stdafx.h, and include the header files you want to include in this header file. Be careful not to include your own header files, only include system header files, and will not change.
  2. Create stdafx.cpp, include stdafx.h in this file
  3. We need to let the compiler know that the stdafx.cpp file is used to create the .pch file. Right click on the stdafx.cpp file --> Properties --> All Configurations --> C/C++ --> Precompiled Header --> Select: Create Precompiled Header (/Yc)
  4. We need to let the compiler know which .cpp files need to use .pch files. Add #include "stdafx.h" in the .cpp that needs to use the .pch file, then right click on the .cpp file --> Properties --> All Configurations --> C/C++ --> Precompiled Header --> Option: Use precompiled headers (/Yu)
  5. Compiling stdafx.cpp alone will generate a .pch file. If you accidentally delete the .pch file in the future, you can compile the stdafx.cpp file separately.

Note:
6. Not all .cpp files must include stdafx.h, and whether to use precompiled headers can be modified in configuration items.
7. If a .cpp file uses a precompiled header file, then include the stdafx.h file at the beginning of the .cpp file, otherwise the #include code before #include “stdafx.h” will be skipped.


Guess you like

Origin blog.csdn.net/zhaopeng01zp/article/details/130322958