Prefix Header
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, a prefix header is a feature found in some C or C++ compilers used to ensure that a certain snippet of code is inserted at the beginning of every file.


Overview

In the C and C++ programming languages, a header file is a
file File or filing may refer to: Mechanical tools and processes * File (tool), a tool used to ''remove'' fine amounts of material from a workpiece **Filing (metalworking), a material removal process in manufacturing ** Nail file, a tool used to gent ...
whose text is included in another source file by the
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
, usually by the use of
compiler directive In computer programming, a directive or pragma (from "pragmatic") is a language construct that specifies how a compiler (or other translator) should process its input. Directives are not part of the grammar of a programming language, and may vary ...
s at the beginning of the source file. A prefix header differs from a normal header file in that it is ''automatically'' included at the beginning of every source file by the compiler, without the use of any compiler directives. Prefix headers are usually pre-compiled in order to reduce compilation times. Use of prefix headers outside of this purpose can make your code more difficult to maintain & less re-usable. Prefix headers can also be used for cross-platform support. On *NIX systems, it is common to have a config.h header file generated at build time (via something like autoconf) that describes the capabilities of the system. However, when using certain build systems such as Visual Studio or Xcode, this config.h may be unavailable. One technique to solve this is to have HAVE_CONFIG_H be a pre-defined macro in the build-system that generates a config.h so that code knows whether it needs to #include config.h (& is safe for use by build systems that do not have it). An alternative, would be for the build system to add config.h as a prefix header instead of defining HAVE_CONFIG_H. Of course the downside is that this header will be added to every compilation unit, not just the ones that include it explicitly.


Example

On
macOS macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and lapt ...
, the
Xcode Xcode is Apple's integrated development environment (IDE) for macOS, used to develop software for macOS, iOS, iPadOS, watchOS, and tvOS. It was initially released in late 2003; the latest stable release is version 14.2, released on December 13, ...
build system generates prefix headers automatically for new projects. A new
Cocoa Cocoa may refer to: Chocolate * Chocolate * ''Theobroma cacao'', the cocoa tree * Cocoa bean, seed of ''Theobroma cacao'' * Chocolate liquor, or cocoa liquor, pure, liquid chocolate extracted from the cocoa bean, including both cocoa butter and ...
project, for instance, gets a prefix header that looks like this: #ifdef __OBJC__ #import #endif As a result, explicit includes of the above header files in any Objective-C code file do not imply a second inclusion because of the #import directive of Objective-C, or more generally with #include because of the use of include guards; hence, these includes can be forgotten, but it is advocated to have them explicitly written in order to keep the source code autonomous and reusable, and make the library dependencies clear. Similar prefix headers are generated for other types of project.


See also

* Header file *
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, espe ...


References

{{Reflist


External links


Xcode User Guide: Using a Precompiled Prefix Header
Source code C (programming language) headers C++ Articles with example Objective-C code