Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Prerequisites
3.
Steps to Compile and Run the C++ Program
3.1.
Using the Command Line or Terminal
3.2.
Using an IDE (Visual Studio Code)
4.
The C++ Compilation Model
4.1.
Pre-Processing
4.2.
Compiling
4.3.
Linking
4.4.
Loading
5.
Frequently Asked Questions
5.1.
What is compile and run in C++?
5.2.
How does C++ compiler work?
5.3.
Which command is used to run C++ program?
5.4.
What is the difference between Compiling and Linking?
5.5.
 What do you mean by Loading?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

How to Compile and Run the C++ Program?

Introduction

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. 

How to Compile and Run the C++ Program?

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’. 

    Recommended Read- Compilers for C++ 
     
  • Text editor or IDE  (VSCode) 

    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.

    For setting up Visual Studio Code for C++, you can refer to ‘Visual Studio Code Setup for C++’.

    Also, if you are a Mac user, refer to - ‘How to setup environment for C++ Programming on Mac.’ for setting up environment in your Mac.

    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).

Code


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.
Output

       Paste the following path in the cmd prompt/ terminal.

Command
  • 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.
 

Command
  • 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.
     
Output

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.
     

Code

Output

  • 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’.
     

Run Option

Command

        →Go to the terminal and paste the path.

Command

        →To run your program, enter ./File_path.

output


Now that we have discussed how to compile and run the C++ program let's discuss the behind-the-scenes of compilation.

Also read, pwd command in linux

The C++ Compilation Model

The C++ program is compiled into an executable program. The compilation model involves stages such as: Pre-Processing, Compiling, Linking, and Loading.

C++ Compilation and Execution

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.

Recommended read-
 

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.

Happy coding!

Live masterclass