VS Code | Compile and Run in C++ (original) (raw)

Last Updated : 11 Feb, 2021

In this article, we will learn how to compile and run C++ program in VS Code. There are two ways of doing that you can use any one of them as per your convenience. It is to be noted that a majority of competitive programmers use C++, therefore the compilation and execution of the program needs to be done quickly. Some methods which are discussed in this article almost automate the process of compilation and execution.

Program:
Let below be the code to demonstrate compilation and execution:

C++ `

// C++ program to take the input of // two numbers and prints its sum #include <bits/stdc++.h> using namespace std;

// Driver Code int main() { int a, b;

// Input two numbers
cin >> a >> b;

// Find the sum
int sum = a + b;

// Print the sum
cout << sum;

return 0;

}

`

Using Integrated Command Line:

For compilation and creation of executable file run the below command:

g++ -std = c++11 -O2 -Wall programName.cpp -o programName.exe

Understanding different terms in above command:

Note: The name of cpp file and executable file need not be same.

Steps:

Method 1 - Calling Executable File and Managing Input/Output

Input/Output in command line itself:

Input/Output through text files:

#ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif

`

Method 2 - Using Code-Runner Extension:

Using the template created so far, we can easily upgrade to code runner. Below are the steps:

Input/Output using Competitive Programming Helper (cph) extension: