Getting Started with C++ and Visual Studio Code on Ubuntu
Before going through these steps make sure you have done Setup C++ development environment on Ubuntu
Visual Studio Code
Download and install from Visual Studio Code site.
Open Visual Studio Code and press Cmd + Shift + P
. Select Shell Command: Install 'code' command in PATH
.
C++ Project
Create a directory called simple in ~/cpp/simple
Open the directory in Visual Studio Code:
Install the C/C++ Extension Pack.
Project Files
Add the following files:
src/main.cpp
CMakeLists.txt
build.sh
#!/usr/bin/env bash
mkdir -p ./build/debug
pushd ./build/debug
cmake -G 'Ninja' -DCMAKE_BUILD_TYPE=Debug ../.. && \
ninja
ret=$?
popd
Make it executable:
.gitignore
Test the build
Open Terminal in Visual Studio Code and test the build from command line:
Automate the build
Add the following Visual Studio Code specific files to the .vscode
subdir:
.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"linux": {
"command": "${workspaceFolder}/build.sh",
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Test the build by pressing Ctrl + B
. Visual Studio Code should execute the build.sh
script automatically.
Setup Debugging
Add the following Visual Studio Code specific files to the .vscode
subdir:
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch (gdb)",
"type": "cppdbg",
"request": "launch",
"cwd": "${workspaceFolder}",
"linux": {
"program": "${workspaceFolder}/build/debug/simple",
"MIMode": "gdb"
}
}
]
}
Test the debugging
Set a breakpoint on the first line of int main()
inside src/main.cpp
. Press F5
to launch the debugger. It should stop at the breakpoint.