LinuxCommandLibrary

cpp

Preprocess C and C++ source code

SYNOPSIS

cpp [options] inputfile [outputfile]

PARAMETERS

-D name
    Define macro name as '1'.

-D name=definition
    Define macro name as definition.

-U name
    Undefine macro name.

-I directory
    Add directory to include search path.

-include file
    Process file as if #include "file" appeared as the first line of the primary source file.

-M
    Generate make dependencies.

-MM
    Like -M but omit system header dependencies.

-MT target
    Specify the target of the generated make rule.

-P
    Inhibit line number and file name output.

-undef
    Do not predefine any non-standard macros.

-std=standard
    Conform to the specified language standard.

-nostdinc
    Do not search standard system include directories.

-v
    Verbose mode. Show included files and macro definitions.

DESCRIPTION

The cpp command is the C preprocessor. It is a separate program invoked by the C compiler to perform textual transformations on source files before compilation. These transformations include: macro substitution (replacing symbolic names with code fragments), conditional compilation (including or excluding code based on conditions), and file inclusion (inserting the contents of other files). cpp takes C, C++, Objective-C, and Objective-C++ source code as input and produces preprocessed output. This output is typically then fed to the compiler. cpp is a powerful tool for code organization, abstraction, and platform-specific adaptations. While generally invoked automatically by the compiler, it can also be used stand-alone to examine the effects of preprocessing. Understanding cpp is essential for advanced C/C++ development.

The preprocessor interprets directives (lines starting with #) in the source code. Common directives include `#include`, `#define`, `#ifdef`, `#ifndef`, `#if`, `#else`, and `#endif`. These directives control the preprocessor's behavior, allowing for flexible code management and customization. When used stand-alone, the output can be redirected to a file for inspection or further processing. The output of cpp is generally human-readable, showing the source code after macro expansions and conditional compilation decisions have been applied.

CAVEATS

The behavior of cpp can be highly dependent on the specific compiler and system configuration. Macro expansion can sometimes lead to unexpected results, especially with complex macros or incorrect usage.

INVOCATION

Typically, cpp is invoked by the C compiler as the first stage of compilation. However, it can also be invoked directly from the command line to preprocess source files independently. In this case, the output is usually redirected to a file or piped to another program.

DIRECTIVES

Preprocessor directives are commands that begin with a '#' symbol. These directives control the preprocessor's behavior, allowing for features such as conditional compilation (#ifdef, #ifndef, #if, #else, #endif), file inclusion (#include), and macro definitions (#define). Understanding these directives is crucial for effective use of cpp.

HISTORY

The C preprocessor (cpp) has been an integral part of the C language since its early development. Originally a separate program, it has evolved alongside C, adapting to new language features and compiler requirements. Its primary role has always been to handle macro definitions, conditional compilation, and file inclusion, enabling code reuse, abstraction, and platform-specific adaptations. Over time, cpp implementations have been refined to improve efficiency and conformance to language standards. Modern compilers often integrate the preprocessor directly into the compilation process, though the core functionality remains consistent.

SEE ALSO

gcc(1), cc(1), make(1)

Copied to clipboard