- C++, Ubuntu, VSCode

Compile and debug C++ project with Visual Studio Code on Ubuntu


This article aims to record how to compile and debug C++ project with Visual Studio Code on Ubuntu.

Step 1. Install gcc and g++ compiler

To compile and debug for the C++ project on Ubuntu, gcc and g++ compiler are required. With the command below to install gcc and g++ with the latest version:

sudo apt install gcc g++

And after the installation, check the version to confirm whether it can work or not:

gcc --version
g++ --version

Step 2. Install Visual Studio Code

Download Visual Studio Code from the official website: https://code.visualstudio.com/.

Install the deb package with the command below:

sudo dpkg -i <package name>

Step 3. Install C/C++ extension in VSCode

To support C++ language environment, C/C++ extension is required to install to the VSCode editor. Click “Extensions” option in the left bar and search “C/C++” to install the extension.

Step 4. Create a project work folder and open in VSCode

Assume that the project is on the desktop with a folder named “hello”.

cd ~/Desktop
mkdir hello
cd hello
# Open the current dir in VSCode
code .

Step 5. Create a cpp file in the work dir

Create “hello.cpp” file in the “explorer” option of the left bar of the interface as shown below:

Step 6. Configure default build task

Press “Ctrl+Shift+P” and choose the option “Configure default build task”, then choose the option “g++ build active file”, a default build task json “task.json” will be generated, which can be seen in the left bar in “Explorer” option.

Step 7. Configure default launch configuration

Press “F5” on the keyboard and choose the option “C++(GDB/LLDB)” and choose “Default configuration”.

Edit the generated launch.json file, replace the parameter “program” from the below:

"program": "enter program name, for example ${workspaceFolder}/a.out"

to the new one:

"program": "${workspaceFolder}/${fileBasenameNoExtension}"

and then save the launch.json file.

Step 8. Compile and debug

Write codes in the file “hello.cpp”, and press “Ctrl+Shift+B” to compile and build the project, press “F5” to debug the project.

It is OK now to compile and debug the C++ project with Visual Studio Code on Ubuntu.

About Ziqi.Yang394

Read All Posts By Ziqi.Yang394

Leave a Reply

Your email address will not be published. Required fields are marked *