- Unix Commands Reference
- Unix Commands - Home
cpp Command in Linux
cpp command in Linux is a C language processor that is automatically used by your C compiler to transform your source code into a form suitable for compilation. You can use the cpp command with C, C++ and Object-C source codes. It performs tasks like file inclusion, macro expansion, and conditional compilation, and helps you make your code more manageable for the compiler.
Table of Contents
Here is a comprehensive tutorial to understand the various options available with the cpp command −
Syntax of cpp Command
When invoking cpp, you typically do so from the command line or within a build system. Here’s the basic syntax to use the cpp command on Linux system −
cpp [options] input_file output_file
Where, input_file is the name of the source code file that you want to preprocess (usually a C, C++, or Objective-C file). The output_file is the name of the output file where the preprocessed code will be written. While [options] are different flags that can be used with the cpp command to customize the command’s behavior.
Note − The common preprocessor directives are −
- #include − Includes a header file.
- #define − Defines a macro.
- #ifdef, #ifndef, #else, #endif − Conditional compilation directives.
cpp Command Options
Option | Description |
---|---|
-pass-exit-codes | Exit with the highest error code encountered during any phase. |
--help | Show the help information related to the command. |
--target-help | Display command line options specific to the target, including assembler and linker options. |
--version | Shows the compiler version information. |
-dumpspecs | Display all built-in spec strings. |
-dumpversion | Show the compiler version. |
-dumpmachine | Show the target processor for the compiler. |
-foffload=<targets> | Specify offloading targets. |
-print-search-dirs | Show the directories in the search path of the compiler. |
-print-libgcc-file-name | Show the name of the companion library of the compiler. |
-print-file-name=<lib> | Show the full path to the library <lib>. |
-print-program-name=<prog> | Show the full path to the compiler component <prog>. |
-print-multiarch | Show the target normalized GNU triplet, utilized as a component in the library path. |
-print-multi-directory | Show the root directory for the versions of libgcc. |
-print-multi-lib | Show the mapping between multiple library search directories and command line options. |
-print-multi-os-directory | Indicate the directory location of operating system libraries. |
-print-sysroot | Specify the directory where target libraries are located. |
-print-sysroot-headers-suffix | Display the additional path component appended to the sysroot directory to locate header files. |
-Wa, <options> | Forward comma-separated <options> to the assembler. |
-Wp, <options> | Forward comma-separated <options> to the preprocessor. |
-WI, <options> | Forward comma-separated <options> to the linker. |
-Xassembler <arg> | Forward <arg> to the assembler. |
-Xpreprocessor <arg> | Forward <arg> to the preprocessor. |
-Xlinker <arg> | Forward <arg> to the linker. |
-save-temps | Preserves intermediate files for inspection or debugging. |
-save-temps=<arg> | Preserves intermediate files for inspection or debugging. |
-no-canonical-prefixes | Preserves the original form of path specifications without simplification. |
-pipe | Use pipes instead of intermediate files. |
-time | Measures the elapsed time for each individual process involved in the compilation. |
-specs=<file> | Override built-in specs with the contents of <file>. |
-std=<standard> | Assume that the input sources are for <standard>. |
--sysroot=<directory> | Use the <directory> as the root directory for libraries and headers. |
-B <directory> | Add <directory> to the search paths of the compiler. |
-v | Show the programs that are invoked by the compiler. |
-### | Similar to -v, however, the options are quoted and commands are not executed. |
-E | Processes only; skip compilation, assembly and linking. |
-S | Compile only without proceeding to assembly or linking. |
-c | Compile and assemble, however, do not link. |
-o <file> | Direct the command’s output to <file>. |
-pie | Generate a dynamically linked-position |
-shared | Produce a shared library. |
-x <language> | Specify the language for the following input files, acceptable languages include C, C++, and assembler. |
How to Use cpp Command in Linux?
To use cpp command on Linux to preprocess the code, first you are required to create a file with the .c extension. Though it can also handle .cpp for C++ and .m for Objective-C. The file must contain your source code, which will be processed by cpp command.
Let’s create a file named code.c on Linux by using the nano editor −
nano code.c
Inside the code.c file, add you desired code, as an example, we are using the following code −
#include <stdio.h> #define PI 3.14159 int main() { printf("The value of PI is: %f\n", PI); return 0; }
Save your code.c file using CTRL+X, add Y and press Enter, then run the code using the GCC compiler through the following command −
gcc code.c -o PI_code
You can replace input file name code.c and output file name PI_code according to your needs.
Note − If gcc is not installed , you can install it from your default system’s package manager.
After the code is executed, you to run the output file to print the result on the terminal −
./PI_code
Once everything is done, you can simply use the cpp command to preprocess the code and save the resulting output to preprocessed.c.
cpp code.c > preprocessed.c
This will create a file named preprocessed.c containing the code after preprocessing. You can open this file using nano editor or cat command to see the result.
You can also view the preprocessed output on standard output by using the -E option with the cpp command −
cpp -E code.c
To suppress all warning messages, you can use the -W option with the cpp command, for example −
cpp -W code.c
For exploring more options, you can get help from the cpp manual by opening it on the terminal using the below-given command −
man cpp
Conclusion
The cpp command is an essential tool in the C, C++, and Objective-C development process. It acts as a preprocessor by helping you transform your source code into a format suitable for compilation.
In this tutorial, we have provided the basic syntax of the cpp command along with its different options or flags. We have also discussed cpp command usage with the help of an example. Using this example as a base, you can explore more options with the cpp command to tailor the preprocessing process to your specific needs.