How to compile a C++ program in Visual Studio code
A Microsoft C/C++ extension provides cross-platform C and C++ programming on Windows, Linux, and macOS by providing C/C++ support for Visual Studio Code. When you create a file with the *.cpp suffix, additional capabilities are included, like error checking, smart completions and hovers (IntelliSense), and syntax highlighting (colourizationcolorization).
Configure your environment for C++.
Because C++ is a compiled language, your program cannot execute on your computer until its source code has been translated and compiled. Since the development workflow for VS Code as an editor depends on command-line tools, the C/C++ extension does not come with a C++ compiler or debugger. Installing these tools is required, or you can use the ones your machine already has installed.
Microsoft offers a compiler for the C, C++, C++/CLI, and C++/CX programming languages called Microsoft Visual C++ (MSVC). MSVC is a proprietary program that was first released as a stand-alone item before being integrated into Visual Studio and made accessible as trialware and freeware.
Program:
Here is the code to show how it is compiled and run:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
int sum = a + b;
cout << sum;
return 0;
}
Steps:
- Select New Terminal by hovering over the terminal tab.
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code1.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code2.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code3.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:heading {](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code4.png)
Approach 1: Managing Input/Output and Calling the Executable File
Input/Output within the actual command line:
- Enter after passing the executable file to be run.
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code5.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code6.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p>Data input and output via text files:</p>
<!-- /wp:paragraph -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code7.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code8.png)
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code9.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code10.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code11.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p>The development tools and libraries for C++, C, and assembly language that come with Visual Studio for Windows are referred to as Microsoft Visual C++ (MSVC). You can create managed apps and libraries that use the.NET Framework, native Windows desktop and server applications, or apps for the Universal Windows Platform (UWP) with these tools and libraries. Make cross-platform programmes and libraries that function on iOS, Android, Linux, and Windows. With MSVC, you may develop any kind of Windows desktop application, from straightforward console apps to extremely intricate and sophisticated ones. Write operating system components, device drivers, and cross-platform mobile games. Aim for anything in the Azure cloud, from multi-server high-performance computing to the tiniest IoT devices.</p>
<!-- /wp:paragraph -->
<!-- wp:heading {](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code12.png)
Approach 2: Utilizing the Code-Runner Extension
We can just update our code runner using the template that has already been produced. The steps are as follows:
- As indicated below, install the code runner extension:
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code13.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:list -->
<ul class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code14.png)
![How to compile a C++ program in Visual Studio code/>
<!-- /wp:html -->
<!-- wp:heading -->
<h2 class=](https://static.tutorialandexample.com/cpp/how-to-compile-a-cpp-program-in-visual-studio-code15.png)
Code refactoring, comprehensive semantic code understanding and navigation, and IntelliSense code completion are all integrated into Visual Studio Code.
- The toughness gets debugged when the coding gets challenging. We made debugging available because it's frequently the functionality that developers miss the most from a reduced coding experience. With the interactive debugger built into Visual Studio Code, you can step through source code, examine variables, see call stacks, and run commands in the console.
- Additionally, VS Code interacts with build and scripting tools to expedite routine processes and streamline daily workflows. Because VS Code supports Git, you can work with source control—including examining pending changes and diffs—without ever leaving the editor.
- With C++, we can design new programming languages, games, embedded systems, and more. We can also make applications for other operating systems, such as Windows, Mac OSX, and Linux. One of the most well-liked IDEs (integrated development environments) for C++ developers is the robust Visual Studio.