Prefix header

Last updated

In computer programming, 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.

Contents

Overview

In the C and C++ programming languages, a header file is a file whose text is included in another source file by the compiler, usually by the use of compiler directives 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. [1] 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, the Xcode build system generates prefix headers automatically for new projects. A new Cocoa project, for instance, gets a prefix header that looks like this:

#ifdef __OBJC__#import <Cocoa/Cocoa.h>#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 [1] 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

Related Research Articles

Cocoa is Apple's native object-oriented application programming interface (API) for its desktop operating system macOS.

<span class="mw-page-title-main">GNU Autotools</span> Suite of programming tools

The GNU Autotools, also known as the GNU Build System, is a suite of programming tools designed to assist in making source code packages portable to many Unix-like systems.

The C preprocessor is the macro preprocessor for several computer programming languages, such as C, Objective-C, C++, and a variety of Fortran languages. The preprocessor provides inclusion of header files, macro expansions, conditional compilation, and line control.

In software development, distcc is a tool for speeding up compilation of source code by using distributed computing over a computer network. With the right configuration, distcc can dramatically reduce a project's compilation time.

RubyCocoa is a macOS framework that provides a bridge between the Ruby and the Objective-C programming languages, allowing the user to manipulate Objective-C objects from Ruby, and vice versa. It makes it possible to write a Cocoa application completely in Ruby as well as to write an application that mixes Ruby and Objective-C code. An Apple project called MacRuby was under development to replace RubyCocoa in 2008. A proprietary spin-off called RubyMotion was subsequently released in 2012, available for iOS, macOS and Android.

pkg-config is a computer program that defines and supports a unified interface for querying installed libraries for the purpose of compiling software that depends on them. It allows programmers and installation scripts to work without explicit knowledge of detailed library path information. pkg-config was originally designed for Linux, but it is now also available for BSD, Microsoft Windows, macOS, and Solaris.

In the C and C++ programming languages, an #include guard, sometimes called a macro guard, header guard or file guard, is a particular construct used to avoid the problem of double inclusion when dealing with the include directive.

In computer programming, a precompiled header (PCH) is a 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 Apple Developer Tools are a suite of software tools from Apple to aid in making software dynamic titles for the macOS and iOS platforms. The developer tools were formerly included on macOS install media, but are now exclusively distributed over the Internet. As of macOS 10.12, Xcode is available as a free download from the Mac App Store.

<span class="mw-page-title-main">CMake</span> Cross-platform, compiler-independent build system generator

In software development, CMake is cross-platform free and open-source software for build automation, testing, packaging and installation of software by using a compiler-independent method. CMake is not a build system itself; it generates another system's build files. It supports directory hierarchies and applications that depend on multiple libraries. It can invoke native build environments such as Make, Qt Creator, Ninja, Android Studio, Apple's Xcode, and Microsoft Visual Studio. It has minimal dependencies, requiring only a C++ compiler on its own build system.

Single compilation unit (SCU) is a computer programming technique for the C and C++ languages, which reduces compilation time for programs spanning multiple files. Specifically, it allows the compiler to keep data from shared header files, definitions and templates, so that it need not recreate them for each file. It is an instance of program optimization. The technique can be applied to an entire program or to some subset of source files; when applied to an entire program, it is also known as a unity build.

configure script Executable script used when building programs

A configure script is an executable script designed to aid in developing a program to be run on a wide number of different computers. It matches the libraries on the user's computer, with those required by the program before compiling it from its source code.

In C and C++ programming language terminology, a translation unit is the ultimate input to a C or C++ compiler from which an object file is generated. A translation unit roughly consists of a source file after it has been processed by the C preprocessor, meaning that header files listed in #include directives are literally included, sections of code within #ifndef may be included, and macros have been expanded.

Many programming languages and other computer files have a directive, often called include, import, or copy, that causes the contents of the specified file to be inserted into the original file. These included files are called header files or copybooks. They are often used to define the physical layout of program data, pieces of procedural code, and/or forward declarations while promoting encapsulation and the reuse of code or data.

Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard programming language used, supported, and promoted by Apple for developing macOS and iOS applications until the introduction of the Swift programming language in 2014.

Swift is a high-level general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community. Swift compiles to machine code, as it is an LLVM-based compiler. Swift was first released in June 2014, and the Swift toolchain has shipped in Xcode since version 6, released in 2014.

Although C++ is one of the most widespread programming languages, many prominent software engineers criticize C++ for being overly complex and fundamentally flawed. Among the critics have been: Robert Pike, Joshua Bloch, Linus Torvalds, Donald Knuth, Richard Stallman, and Ken Thompson. C++ has been widely adopted and implemented as a systems language through most of its existence. It has been used to build many pieces of very important software.

<span class="mw-page-title-main">Meson (software)</span> Build automation system

Meson is a software tool for automating the building (compiling) of software. Meson is free and open-source software written in Python, under the Apache License 2.0.

<span class="mw-page-title-main">Bazel (software)</span> Software tool that automates software builds and tests

Bazel is a free and open-source software tool used for the automation of building and testing software. Google uses the build tool Blaze internally and released an open-sourced port of the Blaze tool as Bazel, named as an anagram of Blaze. Bazel was first released in March 2015 and was in beta status by September 2015. Version 1.0 was released in October 2019.

In software engineering, a unity build is a method used in C and C++ software development to speed up the compilation of projects by combining multiple translation units into a single one, usually achieved by using include directives to bundle multiple source files into one larger file.

References

  1. 1 2 "4 Ways Precompiled Headers Cripple Your Code • Quality Coding". Quality Coding. 28 February 2012. Retrieved 17 August 2018.