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:
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.
Similar Reads
Output in C++ In this article, we will discuss the very basic and most common I/O operations required for C++ programming. C++ runs on lots of platforms like Windows, Linux, Unix, Mac, etc. This is the most basic method for handling output in C++.The cout is used very often for printing outputs, i.e., on the moni
2 min read
Difference between Preprocessor Directives and Function Templates in C++ Preprocessor Directives are programs that process our source code before compilation. There are a number of steps involved between writing a program and executing a program in C / C++.Below is the program to illustrate the functionality of Function Templates:C++ // C++ program to illustrate // prepr
3 min read
How to Compile a C++ Program Using GCC In C++, the GNU Compiler Collection (GCC) is one of the most popular C/C++ compiler that is used to compile and execute the C and C++ program. In this article, we will learn how to compile a C++ program using GCC. Compiling a C++ Program Using GCC The GNU Compiler Collection (GCC) is a versatile too
2 min read
Structure of C++ Program The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows: Documentation Section:This section comes first and is used to document the logic of the program that the programmer going to code.It can be also used to write for purpo
5 min read
How to Redirect cin and cout to Files in C++? In C++, we often ask for user input and read that input using the cin command and for displaying output on the screen we use the cout command. But we can also redirect the input and output of the cin and cout to the file stream of our choice. In this article, we will look at how to redirect the cin
2 min read