您的位置:首页 > 移动开发

Precompiled Header

2012-02-23 10:38 225 查看
开始学C++……

错误 2
error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "StdAfx.h"”?

于是有几个问题:

为什么VS需要查找预编译头?
添加一个头文件有啥用?而且stdafx.h里边没啥东西……

先贴上吧,以后再总结。下面来自维基百科

In the C and C++ programming
languages, a header file is a file whose text may be automatically included in another source
file by the C preprocessor by the use of a compiler
directive in the source file.

Header files can sometimes contain very large amounts of source code (for instance, the header files 
windows.h
 and 
Cocoa/Cocoa.h
 on Microsoft
Windows and Mac OS X, respectively). This is especially true with the advent of large 'header' libraries that
make extensive use of templates, like the Eigen
math library and the Boost (C++ libraries). They are written almost entirely
as header files that the user #includes, rather than being linked at runtime. Thus each time the user compiles their program, they are essentially recompiling numerous header libraries as well that would ordinarily have been precompiled into shared objects
or dynamic link libraries.

To reduce compilation times, some compilers allow header files to be compiled into a form that is faster for the compiler to process. This intermediate form is known as aprecompiled header, and is commonly held in a file named with the extension .pch or
similar, such as .gch under the GNU
Compiler Collection.

A related feature is the prefix header, which is a file that is automatically included by the compiler
without requiring the use of any compiler directives. Prefix headers are commonly precompiled, but not all precompiled headers are prefix headers.


[edit]


stdafx.h

stdafx.h is a file, generated by Microsoft Visual Studio IDE wizards,
that describes both standard system and project specific include files that are used frequently but hardly
ever change.

Compatible compilers (for example, Visual C++ 6.0 and newer) will precompile
this file to reduce overall compile times. Visual C++ will not compile anything before the
#include "stdafx.h"
 in the source file, unless the compile option 
/Yu'stdafx.h'
 is
unchecked (by default); it assumes all code in the source up to and including that line is already compiled.

The AFX in stdafx.h stands for Application Framework eXtensions. AFX was the original abbreviation for the Microsoft
Foundation Classes (MFC). While the name stdafx.h is used by default, projects may specify an alternative name.

看来这个stdafx.h是起了一个标识的作用,不具有通常.h文件的功能。
重点在于: Visual C++ will not compile anything before the
#include
"stdafx.h"
 in the source file, unless the compile option 
/Yu'stdafx.h'
 is
unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
这个precompiled header是为了减少编译时由于#include带来的不必要的编译的负担。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐