C++ is one of the most popular languages. It provides a powerful and flexible environment for developing applications. If you are a beginner and learning how to code in C++, it is crucial to understand how the C++ program is compiled and run.
Are you stuck with the thought of compiling and running the C++ program? Then you don’t have to worry anymore because, in this article, we will learn how to compile and run the C++ program, and not only that, we will also see the behind-the-scenes of the process.
So get ready to bring your code into action and unleash your creativity.
Prerequisites
Before we move on to the process of compiling and running the C++ program, you must ensure that all the necessary software is installed in the system. To set up the local environment in your system, make sure to install the below two essential software.
C++ compiler
A compiler converts high-level language to machine language. For installing the GCC compiler on Windows, refer to ‘GCC Compiler for Windows’.
IDE (Integrated Development Environment) is a software application using which we can write, manage, edit, and debug our source code through an extensive set of tools. For example, Visual Studio Code is a widely used IDE.
Now that you have ensured the environment is set up, let's move on to the steps needed to compile and run the C++ program.
Steps to Compile and Run the C++ Program
Let's discuss how to compile and run your program.
Using the Command Line or Terminal
You need to have a compiler installed and a C++ program file with a ‘.cpp’ extension (e.g., HelloNinja.cpp) that needs to be compiled. For example, we have a C++ program saved as ‘HelloNinja.cpp’ with path C:/Users/Lenovo/NinjaDocs.
C Drive-> Users-> LENOVO (name of the user account)-> NinjaDocs (subfolder within Users directory).
Note: You can use any code editor of your choice; here, we have used Notepad. Moving on, we will also discuss how to compile and run the C++ program in VS code too.
Step 1
Open a terminal window / cmd prompt if one is a window user.
Step 2
Next, we need to change the directory to a specific path with our .cpp file. For example, the ‘NinjaSource.cpp’ file is saved in C:/Users/Lenovo/NinjaDocs.
Paste the following path in the cmd prompt/ terminal.
Step 3
For compiling the C++ source file using a C++ compiler like MinGW or GCC, you can use the following command:
g++ File_Name.cpp -o Executable_Name
For example, g++ HelloNinja.cpp -o HelloNinja This command is used for generating an executable file depending on the system.
Step 4
After a successful compilation, you can run the executable file by entering the name of your file in the terminal, i.e., in our case, it is ‘HelloNinja’.
Below is the output after executing all the necessary commands.
Using an IDE (Visual Studio Code)
To compile and run the C++ program using Visual Studio Code, it is necessary to ensure
that the correct file path is mentioned in its terminal. For instance, we have created a file named ‘NinjaFile.cpp’ and first saved it at path:
C:\Users\LENOVO\Desktop\NinjaDocs.
So to execute the file, we need to mention its path in the terminal as well. Below is a demonstration.
Step 1: Save the file
Press ‘Ctrl+Shift+S’ to save your file on your desired path.
Step 2: Run the program
Now that the file is saved,to run your C++ program, you need to add the path to VS code terminal. To do this, refer to the following steps.
→Click on the ‘Run’ option at the upper right and select the option ‘Run Code’.
→Go to the terminal and paste the path.
→To run your program, enter ./File_path.
Now that we have discussed how to compile and run the C++ program let's discuss the behind-the-scenes of compilation.
The C++ program is compiled into an executable program. The compilation model involves stages such as: Pre-Processing, Compiling, Linking, and Loading.
Pre-Processing
Below are some of the major points associated with Pre-Processing.
This is the first step in which the pre-processor directives are processed; for example, the instructions starting with ‘#’ (#include <iostream>), the header files, macros, and conditional compilation is performed.
Compiling
Below are some of the major points associated with Compiling.
The preprocessed work is now converted into low-level assembly language where the syntax and semantic analysis are done, and the compiler checks for errors.
Syntax analysis: It means analysis of a programming language’s syntax. (for example, a missing semicolon)
Semantic analysis: It means analyzing the statements and declarations of a program(for example, type mismatch, undeclared variables, misuse of a reserved identifier).
If your program is error-free, then an object code and a set of object files are created.
Linking
Below are some of the major points associated with Linking.
The object files that were produced during compilation are combined by the linker. These files include low-level instructions, and ‘symbols’are defined and referenced. Here each symbol can refer to a global or static variable or a function.
The linker links all the external libraries (for example, the standard template library), and an executable file is generated.
Therefore linker resolves the necessary references across various object files to variables, functions, and objects. The linker is also involved in removing any unused code, thus reducing the size.
Loading
Below are some of the major points associated with Loading.
The loader loads the executable file into memory, but it does not necessarily happen as the next step after linking.
The loading process can occur at a different time, depending on the operating system and runtime environment.
Frequently Asked Questions
What is compile and run in C++?
‘Compile and run in C++’ refers to the process of doing syntax analysis and semantic analysis, generating an executable file by linking all the eternal libraries and executing the executable file. The operating system then transfers the control of the program to the ‘main()’ function in your C++ program, and execution is performed.
How does C++ compiler work?
The compiler performs pre-processing, parsing, semantic analysis, optimization, and code generation. It analysis your source code and checks for any errors. Therefore it converts the source code into a machine language and further, the C++ program is converted into an executable program.
Which command is used to run C++ program?
To run the C++ program in your terminal, you need to make sure to change the directory to the one where your program resides, you can use the ‘cd <File_Path>’ command to change the path and to generate an executable file, the command, ‘g++ File_Name.cpp -o FileName’, is used. Further, to run your program just enter the file name in the command prompt.
What is the difference between Compiling and Linking?
In compiling, the preprocessed work is converted into low-level assembly language where the syntax and semantic analysis are done, and the compiler checks for errors. If your program is error-free, then an object code and a set of object files are created. During linking, The object files that were produced during compilation are combined by the linker.
What do you mean by Loading?
The loader loads the executable file into memory, but it does not necessarily happen as the next step after linking. The loading process can occur at a different time, depending on the operating system and runtime environment.
Conclusion
In this article, we have learned how to compile and run the C++ program, and not only that, we have also seen the behind-the-scenes of the process. So, Now you can execute all your creative C++ programs. To move forward in your journey of learning C++, you can refer to our articles mentioned below and accelerate the process.
To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course. You can read more such descriptive articles on our platform, Coding Ninjas Studio. You will find straightforward explanations of almost every topic on the platform. So take your coding journey to the next level using Coding Ninjas.