Precompiled header
In computer programming, a precompiled header (PCH) is a (C or C++) header file that is compiled into an intermediate form that is faster to process for the compiler. Usage of precompiled headers may significantly reduce compilation time, especially when applied to large header files, header files that include many other header files, or header files that are included in many translation units. The ideas of precompiled headers were central to the development of C++ modules. RationaleIn 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 preprocessor directive in the source file. Header files can sometimes contain very large amounts of source code (for instance, the header files 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 a precompiled header, and is commonly held in a file named with the extension UsageFor example, given a C++ file //header.hpp
...
//source.cpp
#include "header.hpp"
...
When compiling Common implementationsMicrosoft Visual C and C++Microsoft Visual C++ (version 6.0 and newer[citation needed]) can precompile any code, not just headers.[1]
It can do this in two ways: either precompiling all code up to a file whose name matches the
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 was used by default in MSVC projects prior to version 2017, any alternative name may be manually specified. Compatible compilers will precompile this file to reduce overall compile times. Visual C++ will not compile anything before the GCCPrecompiled headers are supported in GCC (3.4 and newer). GCC's approach is similar to these of VC and compatible compilers. GCC saves precompiled versions of header files using a " GCC can only use the precompiled version if the same compiler switches are set as when the header was compiled and it may use at most one. Further, only preprocessor instructions may be placed before the precompiled header (because it must be directly or indirectly included through another normal header, before any compilable code). GCC automatically identifies most header files by their extension. However, if this fails (e.g. because of non-standard header extensions), the clangThe clang compiler added support for PCH in Clang 2.5 / LLVM 2.5 of 2009.[5] The compiler both tokenizes the input source code and performs syntactic and semantic analyses of headers, writing out the compiler's internal generated abstract syntax tree (AST) and symbol table to a precompiled header file.[6] clang's precompiled header scheme, with some improvements such as the ability for one precompiled header to reference another, internally used, precompiled header, also forms the basis for its modules mechanism.[6] It uses the same bitcode file format that is employed by LLVM, encapsulated in clang-specific sections within Common Object File Format or Extensible Linking Format files.[6] C++BuilderIn the default project configuration, the C++Builder compiler implicitly generates precompiled headers for all headers included by a source module until the line In addition, C++Builder can be instrumented to use a specific header file as precompiled header, similar to the mechanism provided by Visual C++. C++Builder 2009 introduces a "Precompiled Header Wizard" which parses all source modules of the project for included header files, classifies them (i.e. excludes header files if they are part of the project or do not have an Include guard) and generates and tests a precompiled header for the specified files automatically. Pretokenized headersA pretokenized header (PTH) is a header file stored in a form that has been run through lexical analysis, but no semantic operations have been done on it. PTH is present in Clang before it supported PCH, and has also been tried in a branch of GCC.[8] Compared to a full PCH mechanism, PTH has the advantages of language (and dialect) independence, as lexical analysis is similar for the C-family languages, and architecture independence, as the same stream of tokens can be used when compiling for different target architectures.[9] It however has the disadvantage of not going any further than simple lexical analysis, requiring that syntactic and semantic analysis of the token stream be performed with every compilation. In addition, the time to compile scaling linearly with the size, in lexical tokens, of the pretokenized file, which is not necessarily the case for a fully-fledged precompilation mechanism (PCH in clang allows random access).[9] Clang's pretokenization mechanism includes several minor mechanisms for assisting the pre-processor: caching of file existence and datestamp information, and recording inclusion guards so that guarded code can be quickly skipped over.[9] ModulesModules, introduced in C++20, function similarly to precompiled headers, and are compiled to .pcm (precompiled module) files, similar to .pch files for precompiled headers. Modules allow greater encapsulation and control of exported symbols, and like precompiled headers allow for faster compilation.[10] Modules are handled entirely by the compiler rather than the preprocessor, and thus unlike precompiled headers, cannot export macros due to being handled after the preprocessing step. Since C++23, the C++ standard library is provided as a module.[11] See alsoNotesReferences
External links |
Portal di Ensiklopedia Dunia