LinuxCommandLibrary

cpp

Preprocess C and C++ source code

SYNOPSIS

cpp [option...] [input-file...] [-o output-file]

PARAMETERS

-D name[=defn]
    Define macro name as defn (default 1)

-U name
    Undefine macro name

-I directory
    Add directory to header search path

-include file
    Include file as if #include "file" at start

-nostdinc
    Ignore standard include directories

-undef
    Undefine all predefined macros

-dD
    Dump all macro definitions to stdout

-dM
    Dump macros like -dD without preprocessing

-M
    Generate Makefile dependency rules

-MM
    Like -M, ignore system headers

-MG
    Like -MM, assume missing headers exist

-P
    Disable #line directive generation

-C
    Preserve comments in output

-traditional
    Enable traditional CPP mode

-v
    Verbose output with search paths

-o file
    Write output to file

--help
    Print usage summary

--version
    Print version information

DESCRIPTION

The cpp command is the GNU C Preprocessor, essential for processing C, C++, and Objective-C source code before compilation. It handles directives such as #include for file inclusion, #define for macro definition, #ifdef/#ifndef for conditional compilation, and #line for source location tracking. Cpp expands macros, removes comments (unless -C), and substitutes defined symbols, producing preprocessed output suitable for the compiler.

Invoked standalone or via compilers like gcc -E, it searches include paths for headers, supports system-specific macro predefinitions, and offers modes for dependency generation useful in Makefiles. Gnu extensions provide advanced features like computed includes and assertion handling. Widely used in build systems, it ensures portable preprocessing across standards from ANSI C to C11/C++17.

Cpp processes one or more input files sequentially or stdin if none specified, directing output to stdout or a specified file.

CAVEATS

Specific to C-family languages; deep recursion or large files may cause stack overflow or high memory use. GNU extensions may reduce portability. Use gcc -E for compiler-integrated preprocessing.

EXAMPLES

cpp file.c   Preprocess to stdout.
cpp -o file.i file.c   Output to file.i.
cpp -DMACRO=1 -I/usr/include file.c   Define macro, add include path.

EXIT STATUS

0 on success, non-zero on errors like unterminated macros or include failures.

HISTORY

Developed by Richard Stallman as part of GCC in 1987. Evolved with GCC releases to support ANSI C (1989), C99, C11, C++ standards, and Objective-C. Maintained by GNU Project, with ongoing enhancements for modern compilers.

SEE ALSO

gcc(1), g++(1), as(1), ld(1), make(1)

Copied to clipboard