Run, Debug & get IntelliSense for C C++ in VSCode
Tags: vscode, cpp, c
Published: 2019-11-13 16:42:21

---
title: Run, Debug & get IntelliSense for C C++ in VSCode
published: true
date: 2018-04-24 21:07:05 UTC
tags: vscode,cpp,c
canonical_url: https://gourav.io/blog/setup-vscode-to-run-debug-c-cpp-code
---



![](https://cdn-images-1.medium.com/max/1024/1*am2R0KHSgYdw8KOTPk37Hg.png)

**You can read the updated article on my blog [here](https://gourav.io/blog/setup-vscode-to-run-debug-c-cpp-code)**

---

> By the end of this short guide you’d be able to run, debug and get intelliSense for C/C++ files in VSCode. Though, this guide is focused for Windows platform but can be extended to Mac and Linux with some minor changes.

I extensively used C & C++ in my [competitive programming] (https://en.wikipedia.org/wiki/Competitive_programming) years and wanted a better support for debugging & intellisense. Only options available were [Dev-C++](orwelldevcpp.blogspot.com)(outdated) and the original "Mammoth" Visual Studio.
Lately, I found VScode and fell in love with it (Atom also only till I found VScode). I tweaked around and set it up as complete IDE For small C, C++ projects especially geared towards competitive programming.

### Create a sample C/C++ project

1. Open/Create an empty folder in [VSCode](https://code.visualstudio.com/).
2. Create a new.cpp file inside it like below:

```
#include \<iostream\>
using namespace std;
int main()
{
 cout \<\< "Hello World" \<\< endl;
 // uncomment below line to stop cmd from exiting immediately in case of "externalConsole": true
 //system("pause");
 return 0;
}
```

3. Install _recommended_ C/C++ extension in VSCode and reload.

![](https://cdn-images-1.medium.com/max/960/1*4fKHWJcmJmdeG66ai2E9bQ.png)<figcaption>Official C/C++ Extension for VSCode</figcaption>

### Install C/C++ Compiler

C/C++ extension **does not include** a C++ compiler. So, you will need to install one or use which is already installed on your computer.

**Windows:** Download [MinGW64.zip](https://github.com/JerryGoyal/MinGW64/releases) (latest release) and extract it to the C Drive.

**Mac:** [XCode](https://developer.apple.com/xcode/)

**Linux:** [GCC](https://gcc.gnu.org/)

Also, Make sure to add C++ compiler PATH to environment variable of your platform. For Windows MinGW64 add: C:\MinGW64\bin

### Enable IntelliSense

> UPDATE: No configuration required in latest release, IntelliSense is enabled by default :)

### Run and Debug C/C++ Code

You’ll notice that there is also a  **.vscode** folder in your sample project. To configure ‘debug configuration’ 2 files are required launch.json and tasks.json inside .vscode folder.

VSCode can create and auto-configure these files if we try to debug for the first time. To do that, open C++ file in VSCode and either hit **F5** or go to _Debug -\> Start Debugging_ and select C++ (GDB/LLDB) then select g++.exe build and debug active file.

![](https://cdn-images-1.medium.com/max/936/1*n7-rARwPBxEwtLF3GW1Wvw.png)<figcaption>Select C++ (GDB/LLDB)</figcaption>

![](https://cdn-images-1.medium.com/max/914/1*qBxW08EA-FbfCpptmibcNQ.png)<figcaption>Select g++.exe build and debug active file</figcaption>

This should create 2 files launch.json and tasks.json in .vscode folder which should look like below (update the MinGW64 path if not correct)

Notice that I’ve added one more optional configuration g++ build & run active file in launch.json and g++ build & run in tasks.json file for purpose of also **Running C/C++ code without debugging**. Now you may choose which configuration to pick when you start debugging. You may remove the configuration whichever you won’t need.

![](https://cdn-images-1.medium.com/max/559/1*zVnhLFwqnNg-kuaLhqDgow.png)<figcaption>Run &amp; Debug or Only run code</figcaption>

#### launch.json

```
{
 "version": "0.2.0",
 "configurations": [
 {
 "name": "g++.exe build and debug active file",
 "type": "cppdbg",
 "request": "launch",
 "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
 "args": [],
 "stopAtEntry": false,
 "cwd": "${workspaceFolder}",
 "environment": [],
 "externalConsole": false, //set to true to see output in cmd instead
 "MIMode": "gdb",
 "miDebuggerPath": "C:\\MinGW64\\bin\\gdb.exe",
 "setupCommands": [
 {
 "description": "Enable pretty-printing for gdb",
 "text": "-enable-pretty-printing",
 "ignoreFailures": true
 }
 ],
 "preLaunchTask": "g++.exe build active file"
 },
 {
 "name": "g++ build & run active file",
 "type": "cppdbg",
 "request": "launch",
 "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
 "args": [],
 "stopAtEntry": false,
 "cwd": "${workspaceFolder}",
 "environment": [],
 "externalConsole": false, //set to true to see output in cmd instead
 "MIMode": "gdb",
 "miDebuggerPath": "C:\\MinGW64\\bin\\gdb.exe",
 "setupCommands": [
 {
 "description": "Enable pretty-printing for gdb",
 "text": "-enable-pretty-printing",
 "ignoreFailures": true
 }
 ],
 "preLaunchTask": "g++ build & run active file"
 }
 ]
}
```

#### tasks.json

```
{
 "tasks": [
 {
 "type": "shell",
 "label": "g++.exe build active file",
 "command": "C:\\MinGW64\\bin\\g++.exe",
 "args": [
 "-g",
 "${file}",
 "-o",
 "${fileDirname}\\${fileBasenameNoExtension}.exe"
 ],
 "options": {
 "cwd": "C:\\MinGW64\\bin"
 }
 },
 {
 "type": "shell",
 "label": "g++ build & run active file",
 "command": "C:\\MinGW64\\bin\\g++.exe",
 "args": [
 "${file}",
 "-o",
 "${fileDirname}\\${fileBasenameNoExtension}.exe"
 ],
 "options": {
 "cwd": "C:\\MinGW64\\bin"
 }
 }
 ],
 "version": "2.0.0"
}
```

> externalConsole in launch.json can be set to true to see code output in cmd instead.

**Restart VSCode** to take effects of newly added compiler paths.

Open any C/C++ file, set some breakpoints (or not) and hit the Big Green Play Button.

> _(Shortcut to debug: _ **_F5_** _)_

![](https://cdn-images-1.medium.com/max/1024/1*9cwk-zFyRakslR73wVVTJg.png)

**Tip** : To hide \*.exe files in the side explorer of VSCode, open settings and paste the below config:

```
"files.exclude": {
 "\*.exe": true
 }
```

---
#### Prefer to work in JAVA?
 

I’ve also added an answer [here](https://stackoverflow.com/questions/39582491/debug-run-standard-java-in-visual-studio-code-ide-and-os-x/46128218#46128218) on how to run and debug Java code in VSCode.

---

Thanks for reading, you can also follow me on Twitter [@GorvGoyl](https://twitter.com/@GorvGoyl)