Are you struggling to figure out where to start C++ development on Linux (Ubuntu)? This post introduces practical setup methods for C++ development in Ubuntu. In particular, it helps you get acquainted with a development environment capable of building, running, and debugging using VSCode.
While many use Visual Studio on Windows, Linux requires alternative setups.
This document summarizes how to establish an environment for C++ development on Ubuntu Linux, setting up debugging and build automation in VSCode.
It's structured step-by-step to make it easy for beginners in C++ to follow along.
1. Install Required Packages
sudo apt update
sudo apt install build-essential gdb
build-essential
includes the following:
g++
: C++ compilergcc
: C compilermake
: Build automation tooldpkg-dev
etc.
2. Install VSCode (Snap Version)
sudo snap install code --classic
Alternatively, you can install it directly from https://code.visualstudio.com
3. Install VSCode Extensions
- C/C++ (by Microsoft)
- (Optional) Code Runner: For quick code execution
4. Create Project Directory and Prepare Example File
mkdir ~/cpp_projects/helloworld
cd ~/cpp_projects/helloworld
code .
Create example file main.cpp
:
#include <iostream>
int main() {
std::cout << "Hello, C++!" << std::endl;
return 0;
}
5. Configure .vscode/tasks.json
(Automatic Build)
{
"version": "2.0.0",
"tasks": [
{
"label": "build C++ file",
"type": "shell",
"command": "g++",
"args": [
"-g",
"main.cpp",
"-o",
"main"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "Compile main.cpp to main"
}
]
}
You can build with
Ctrl + Shift + B
6. Configure .vscode/launch.json
(Debugging)
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/main",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build C++ file",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
You can start debugging using
F5
.
It automatically compiles and enters the debugging mode.
7. Summary of Compilation and Execution
Action | Shortcut Key |
---|---|
Build (Compile) | Ctrl + Shift + B |
Run and Debug | F5 |
Execute Terminal | ./main (when run directly) |
8. Additional Tips
- The command
g++ -g main.cpp -o main
compiles with debugging information included. - If adding other
.cpp
files, you need to modify the file names inargs
. - If you have multiple files, you can extend the setup with Makefile or CMake.
References
- Official VSCode C++ Documentation
- gdb command tutorial
- cppreference - Language types
Author's Note
This environment can be easily replicated anywhere on Linux-based systems like personal servers, Raspberry Pi, and GCP VMs. If integrated with Python or C++ AI modules, this structure provides a powerful foundation.
Add a New Comment