Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Preprocessor output of cpp file

  • Last Updated : 19 Jan, 2021

Preprocessing is a stage where preprocessor directives are expanded or processed before source code is sent to the compiler. The most common example of such directive is #include or #define. A preprocessor output has “.i” extension.

Two files are created here:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

C++




// C++ program for the test.cpp
#include "MyHeader.h"
#include <iostream>
using namespace std;
#define VAL 9
  
// Driver Code
int main()
{
    // powerOfTwo function is defined
    // in “MyHeader.h”
    cout << powerOfTwo(VAL) << endl;
  
    return 0;
}

MyHeader.h




// Below is code for the header file
// named as "MyHeader.h"
#pragma once
  
// Function to find the value of 2^x
size_t powerOfTwo(const int x)
{
  
    // return the value
    return 1 << x;
}

Now on command prompt, the output can be preprocessed in two ways:



Method 1 – inside cmd.exe: This method is not recommended.

g++ -E test.cpp

Here, output can’t be shown as header file iostream will expand up to 50000 lines. This will print all the data on the command prompt which might take a few moments as cout is quite slow in printing. Press any key to stop printing on prompt. The command prompt doesn’t retain all 50000 lines. It has its own limits after which earlier outputs can’t be seen.

Method 2 – Create an explicit file “.i”:

Use the below-given command to get hello.i

g++ -E test.cpp -o hello.i

Output:

Explanation: In the above output, there are only 26000 lines. It is normal as iostream is a big header file and also includes many other header files that do include others. #include directive just copies the content of respective header files.
As noticed, the actual program lies at the very end at 26527 lines but still, it gives error messages for the correct line. Also, all content of multiple files is wrapped into hello.i but still error messages received for correct file context.

How does this happen?

# 7 "MyHeader.h"

This is the line control directive. It will tell the compiler that the next line is the 7th line in the file MyHeader.h. If the above code is executed, then the function powerOfTwo() is defined at line number 7. Also, macro VAL is completely replaced by 9.

Few Interesting things:

  • The preprocessor output file can be compiled using the below command:
g++ hello.i -o test.exe
  • Preprocessing of include directive :
// test.cpp
#include<iostream>

int main(){
  std::cout << "GeekForGeeks" << std::endl
  #include"MyHeader.h"
}
// MyHeader.h
;

In the above example, the semi-colon is copied and pasted from MyHeader.h to the line next to cout statement by #include directive. Hence, no error will generate here. Also, Myheader.h can be a text file, a python file, or HTML file no matter what, #include will just copy it.




My Personal Notes arrow_drop_up
Recommended Articles
Page :