Open In App

cpp command in Linux with Examples

Last Updated : 15 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

cpp is the C language preprocessor, it is automatically used by your C compiler to transform your program before compilation. It is also termed as a macro processor because it is used to give abbreviations for the longer piece of code. It can only be used with C, C++ and Objective-C source code. Using with other programming languages may cause uncertain problems.

Syntax:

cpp  [-options]  infile  outfile

Some Important Options:

  • -D name : Predefine name as a macro.
  • -D name=definition : The contents of definition are tokenized and processed as if they are written in program itself.
  • -U name : Cancel any previous definition of macro.
  • -undef : Do not predefine any system-specific or GCC-specific macros. The standard predefined macros remain defined.
  • -I dir : Add the directory dir to the list of directories to be searched for header files.
  • -Wall : Turns on all optional warnings which are desirable for normal code.
  • -Wcomments : Warn whenever a comment-start sequence /* appears in a /* comment, or whenever a backslash-newline appears in a // comment.
  • -Wendif-labels : Warn whenever an #else or an #endif are followed by text.
  • -w : Suppress all warnings, including those which GNU CPP issues by default.
  • -M : Instead of outputting the result of preprocessing, output a rule suitable for make describing the dependencies of the main source file.
  • -MM : Like -M but do not mention header files that are found in system header directories.
  • -x c
  • -x c++
  • -x objective-c
  • -x assembler-with-cpp : All four above Specify the source language: C, C++, Objective-C, or assembly. This has nothing to do with standards conformance or extensions; it merely selects which base syntax to expect.

Examples: We have created two codes to explain the concept we will refer them as code_a.c and code_b.c.

#include
void main()
{
  printf("Hello, World!");
}

code_a.c
#include
void main()
{
   printf(out);
}

code_b.c
  • Using cpp command :

    It will result into the following output:

    # 1 "code_a.c"
    # 1 ""
    # 1 ""
    # 1 "/usr/include/stdc-predef.h" 1 3 4
    # 1 "" 2
    # 1 "code_a.c"
    # 1 "/usr/include/stdio.h" 1 3 4
    # 27 "/usr/include/stdio.h" 3 4
    # 1 "/usr/include/features.h" 1 3 4
    # 367 "/usr/include/features.h" 3 4
    # 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4
    # 410 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 3 4
    # 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4
    # 411 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4
    # 368 "/usr/include/features.h" 2 3 4
    # 391 "/usr/include/features.h" 3 4
    ..........
    .............
    ....................
    # 2 "code_a.c"
    void main()
    {
    printf("Hello, World!");
    
    }
    

    The output is too large and we actually don’t need it to understand the concept. The thing we understood is that it simply called and replaced the whole piece of code in the header files to the program.

  • using -D option :

    # 1 "code_b.c"
    # 1 ""
    # 1 ""
    # 1 "/usr/include/stdc-predef.h" 1 3 4
    # 1 "" 2
    # 1 "code_a.c"
    # 1 "/usr/include/stdio.h" 1 3 4
    # 27 "/usr/include/stdio.h" 3 4
    # 1 "/usr/include/features.h" 1 3 4
    # 367 "/usr/include/features.h" 3 4
    # 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4
    # 410 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 3 4
    # 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4
    # 411 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4
    # 368 "/usr/include/features.h" 2 3 4
    # 391 "/usr/include/features.h" 3 4
    ..........
    .............
    ....................
    # 2 "code_b.c"
    void main()
    {
    printf("Hello, World!");
    
    }
    
    

    Observe that it prints the same result. This is because it used the macro we declared on the command line.

  • Using -M option:

    Observed the difference. This is because it only outputs the rules required for make.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads