Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Compile and Run C++ Program



The C++ programming language is a set of instructions that tells the compiler how to perform specific tasks. These instructions consist of functions and statements, which, when compiled and executed, tell the computer what action to perform. 

Prerequisite

To compile and execute the program following prerequisites need to be met.

  • Compiler: Any compiler downloaded and installed, like GCC, Clang, or MSVC, or an IDE (like Visual Studio Code, Code::Blocks, or Eclipse).
  • Source program: And a source program file saved as name.cpp (example: Myfile.cpp)

Instructions

Here are the following instructions to compile and run the program.

Step 1: Open a Terminal or Command Prompt

In this, you have to open the command line interface (CLI) of your operating system.

For Windows: Use Command Prompt (type cmd in the search bar) or PowerShell

For Mac\Linux: Open Terminal app

Step 2: Change the Directory to Where Your C++ File is Stored

For this, you need to write a command. Here, cd stands for "change directory".

For Windows Command Prompt (CMD) and PowerShell: 

cd C:\Users\<yourUsername>\Documents</yourusername>

For Linux/Mac (Terminal):

cd /home/<YourUsername>/Documents

For example, if your file is saved in C:/Users/Dell/Documents, then you will type

cd C:/Users/Dell/Documents

Step 3: Compile Your C++ Source Code

Assume you have a C++ file saved a Myfile.cpp; therefore, you will write the following command next. This command is the same for all Windows, Linux, and Mac. 

$ g++ -o my_program Myfile.cpp

Step 4: Run the Program

Finally, just run the given program using the command. Here, ./ means the current directory.

$ ./my_program
Updated on: 2025-05-12T19:38:32+05:30

170K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements