Pre-compiled files, precompiled header files recognize

Source: Internet
Author: User

First, the concept:
1, precompiled: Is the compiler first compile a file (called a precompiled Header file), and then save the compilation results, and then if there are other source files include this "precompiled header File", then automatically extract the required information from the compilation results compiled.
2. Precompiled result file (precompiled header File): The file that is used to save the compiled symbol information (. PCH as suffix)
3, generate the precompiled result file (Create Precompiled Header File): We say that source file A through file B "Generate precompiled Result file" refers to compile a when compiling the results of compilation B is saved to the precompiled result file. General use of the wizard, a file is "Stdafx.cpp", b file is "stdafx.h". Stdafx.cpp in the line statement:
#include "stdafx.h"
4. Use precompiled header (using precompiled header): We say that a source file (A.cpp) uses "stdafx.h" to compile a.cpp, if the A.cpp first line include statement is # include " StdAfx.h ", then directly take the result of the precompiled result file, no longer recompile" stdafx.h "
Ii. How does the wizard do it?
1. The precompiled option to set "Stdafx.cpp" is to "generate the precompiled result file" through the "stdafx.h" file.
2. The precompiled option setting for other source files is "stdafx.h" to "Use precompiled Header"

Iii. principles of Use?
1, the relatively stable header files (such as CRT,STL, third-party fixed library) are all written in the stdafx.h. (whether to use stdafx.h depends on personal preferences, but use stdafx.h can be consistent with the wizard)
2. The first line of all source files is add # include "StdAfx.h".
3, some can not modify the source file (if the public code, do not have permission to modify the code), set his pre-compilation option is "Do not use precompiled header." Note that you must not select "Automatically generate precompiled headers" because this will flush out the results of stdafx.h (this is not a bug or a design problem,. ^_^. )。
Iv. Q&a
Q, why not use the "Automatically generate precompiled header file"?
A, "Automatic generation of precompiled header Files" and nothing is no different, the compilation speed is not improved.

Q, when manually adding a new source file to the project, a similar error often occurs:
Fatal error c1010:unexpected end of file while looking for precompiled header directive
A, because the wizard default setting is "Use Precompiled header", but your newly added file does not contain "stdafx.h" in the first line. The workaround is either modified to "do not use precompiled headers" or add a line of # include "StdAfx.h"

Q, plus stdafx.h and stdafx.cpp always feel that the compiler is bound to the platform, not portability?
A, in fact, pay attention to the wording of stdafx.h no problem. My solution is to

(contents of stdafx.h):
–begin of File StdAfx.h
#ifdef _WIN32
#include "win.h"//refers to the Common header file under window
#else
#include "linux.h"//Refers to common header files under Linux
#endif

#include "crt.h"//refers to C standard library
#include "stl.h"//Generic STL Library
–end of File

Or more simply, if not the VC compiler, then stdafx.h will not write any content!

Pre-compiled files, precompiled header files recognize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.