Compiling C++ code in VS Code

Vinay Dagar
5 min readJan 18, 2021

C++ is a great programming language to start with, yeah it may be a bit hard to catch up in the start if you are a complete beginner, but you'll definitely love C++ once you get to know its real power and all the capabilities.

C++ is also my personal favorite. It’s a general-purpose, statically-typed, middle-level programming language. Wait, what? Middle-level programming language? What does this middle-level mean? The middle-level language lies in between the low level and high-level language. Here is the link to a great answer to why C++ is a mid-level language?

Now without further due, let's dive in on How to compile C++ code in our favorite visual studio code?

Installing a Compiler

C++ is a compiled language, which means the C++ code must be compiled before it can run on the computer. To compile the C++ code we need a compiler if you don’t have one already, you can get one from here.

MinGW is a free and open-source software development environment to create Microsoft Windows applications.

Installing MinGW is pretty simple and straight forward, we just need to select x86_64 Architecture on the second screen i.e.,

  1. Click next to proceed to installation.

2. In Architecture select x86_64 and then select Next.

3. On the Installation Folder page, you can use the default location for the Destination folder. But the general recommendation is installing in C Drive instead of C: Program Files. Copy the location as you will need it later to set the environment variable.

4. Now after the installation is done, we need to set the path of the bin folder to the environment variables.

To open the environment wizard just type “Edit Environment” in the search box

Congratulations MinGW has been installed successfully! To verify the installation, just open the terminal or command prompt and try following commands

g++ --version
gdb --version

It should look like this based on the version you’ve installed mine is 8.1.0

Now you are ready to use C++ inside VS-Code.

Create c++ file in vscode

Create a folder on your pc anywhere, and open vs-code in the newly created folder, and create a file with extension .cpp

Now write your c++ code in the file, I’m just using a simple hello world program.

#include <iostream>

using namespace std;

int main()
{
cout << "Hello World" << endl;
}

Now, we need to build this c++ program, from the Terminal tab, select Run Build Task, i.e.,

it will prompt the various compiler option, and as we’ve installed MinGW we’ll use C/C++: g++.exe build active file.

Selecting this option will start the build in the integrated terminal, and creates a new file with .exe extension with the same name as the CPP file

Now you can run the newly created file in this case first.exe from the terminal.

Congratulations! you just compiled your first c++ program in vs-code.

Isn’t this method a bit time-consuming?

If we want to test our code more frequently, then this method will not be reliable much. So what should we do then? There is a simple and easy solution to this problem.

We just need to add one extension in our Vs-Code i.e., C/C++ Compile Run here is the direct link for this extension or just type compile run it should be the first, if not scroll a bit

just install it, I’ve already installed it as you can see, after installing this should show a play icon on the top right of the editor tab

Now it's just a one-click to compile our C/C++ code on the go, or we can press “Ctrl+Alt+N” for the short-cut. clicking on the play icon will open the command prompt and compile the C++ code

In case it opens the external terminal, we can change that too, from the settings->Extensions->Compile Run Config, just un-check the Run-in-external-terminal checkbox. and you are good to go.

Now you are all set up, to deep dive into C++ in our favorite code editor Vs-Code.

Happy coding! 😊

--

--