How to Run and Debug C++ with Visual Studio Code on Debian
Visual Studio Code is a popular source code editor among many developers. This tutorial is going to show you how to run and debug lightweight C++ codes in it.
Since my desktop runs on Debian, you might need to make a few minor changes for Windows PC and macOS.
Install Compiler g++
sudo apt install build-essential
Run C++ Code
This can be done by installing Code Runner extension.
If your program needs to take user input, we can modify a few things in VSCode user settings.
Initiate Command Palette by Ctrl + Shift + P
and search Open User Settings
. If you would like to make this change globally, click the curly bracket on the right corner. In case you prefer this change only within the current workspace, select Workspace
tab first and then click the curly bracket.
Add the following line.
"code-runner.runInTerminal": true,
Furthermore, we can expand the settings to support newer functions introduced in C++14 and C++17 by appending one more line.
"code-runner.executorMap": {
"cpp": "cd $dir && g++ -std=c++14 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
},
You should be able to compile your .cpp
files and generate executable programs now.
Debug C++ Code
Here comes another question: how can I utilize VSCode debug option with C++ codes?
First of all, let us check if our system has GNU Debugger installed.
sudo apt install gdb
We also need to install another VSCode extension C/C++.
Now, we can open a .cpp
file and hit F5
.
Select Select C++ (GDB/LLDB)
Select g++ build and debug active file
.
Two json
files will be created in directory .vscode
- launch.json
and tasks.json
. There is no need to change any default settings in those two files. You should be ready to debug C++ source codes by F5
now.
I hope you liked this short tutorial. Stay tuned by signing up for my newsletter. If you have any questions/comments/proposals, feel free to shoot me a message on Twitter/Discord/Patreon.
Happy coding!