TL;DR
./scripts/run.shProject Hierarchy
includes/ → Header files (.h, .hpp)
src/ → Source files (.cpp)
tests/ → GoogleTest test cases
Make sure the following tools are installed before building the project:
- g++ / gcc
- CMake
- Git
- lcov (for code coverage)
- cppcheck (for static analysis)
- Optional: Install
clang-formatto format C++ code.
Linux
sudo apt install clang-format
find . -regex '.*\.\(cpp\|h\|hpp\)' -exec clang-format -i {} \;Windows
# Install clang-format via Chocolatey
choco install clang-format
# Apply clang-format recursively to .cpp, .h, .hpp files
Get-ChildItem -Recurse -Include *.cpp, *.h, *.hpp | ForEach-Object { clang-format -i $_.FullName }- Ubuntu system
- Install
gcc,cmake,git, andpthread(Skip this step if you already install)$ sudo apt-get update $ sudo apt-get install g++ $ sudo apt-get install lcov $ sudo apt-get install cmake $ sudo apt-get install git $ sudo apt install valgrind $ sudo apt-get install cppcheck $ sudo apt-get install -y clang-tidy $ sudo apt install python3-gcovr
- Build the application and the tests
$ cd build $ cmake .. $ cmake --build .
- Run the application and the test
$ ./bin/cpp_lab_project $ ./bin/cpp_lab_project_test
- Detect Memory Leak Using valgrind
$ valgrind --leak-check=full -v ./cpp-lab
- (Optional) Run static analysis - cppcheck
$ cppcheck "folder" / "file"
- (Optional) Run static analysis - clang-tidy
$ clang-tidy -p build -header-filter='^src/.*' $(find src -name "*.cpp")
- (Optional) Run coverage - lcov
$ ./scripts/gen_coverage_lcov.sh
- Install
- Docker
- Update
Dockerfile - Build the Docker image
$ cd build $ docker build --tag cpp-lab . - Run an interactive container
$ docker run -it cpp-lab:latest /bin/bash - Inspect the environment
$ printenv - Notes:
- Use the
-tor--tagflag to set the name of the image to be created. (the full name is actuallycpp-lab:latest, since latest is the default tag) - Opening an interactive shell inside your Docker container to explore, test, or debug the environment built from your image.
- docker run to start a new container.
-it: run it interactively:-i: keep STDIN open (so you can type commands)-t: allocate a terminal (TTY)cpp-lab:latest: the image you built earlier./bin/bash: the command to execute inside the container (opens a Bash shell).
- Use the
- Update
To debug C/C++ projects in VS Code, you must create a launch configuration that specifies:
- The application entry point (executable)
- How to attach to a running process
- Environment variables needed
- Saved debugging setup details
All launch configs are stored in .vscode/launch.json within your workspace.
- Go to
Run→Add Configuration...or - Use VS Code chat command:
to auto-generate a debug configuration.
/startDebugging
You can start a debug session in several ways:
- F5
- Ctrl + Shift + D: Open Debug panel, select a config, press Start
- Ctrl + Shift + P → select
Debug: Select and Start Debugging
The most important field is the executable that will run:
// .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/debug/bin/cpp_lab_project",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "CMake Debug Build",
"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
}
]
}
]
}An example tasks.json for CMake builds:
// .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "CMake Debug Configure",
"type": "shell",
"command": "cmake",
"args": [
"-S",
".",
"-B",
"build/debug",
"-DCMAKE_BUILD_TYPE=Debug"
]
},
{
"label": "CMake Debug Build",
"type": "shell",
"command": "cmake",
"args": [
"--build",
"build/debug"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always"
},
"dependsOn": "CMake Debug Configure"
}
]
}- Open your project in VS Code
- Install Extension:
- C/C++ Extension Pack
- Install GDB:
sudo apt update sudo apt install gdb
- Start Debugging:
PressF5 - Run Without Debugging:
PressCtrl + F5 - Build Project (Optional):
PressCtrl + Shift + B
Notes:
| Shortcut | Action |
|---|---|
| F5 | Start debugging |
| Ctrl + F5 | Run without debugging |
| Ctrl + Shift + D | Open Debug panel |
| Ctrl + Shift + B | Build project |
TBD - Refer to this Documentation with doxygen
# Navigate to the project that contain your Dockerfile
cd cpp-lab
# Build the project by running the following command, swapping out DOCKER_USERNAME with your username.
docker build -t DOCKER_USERNAME/cpp-lab .
# Verify the image exists locally
docker image ls
# To push the image
docker push DOCKER_USERNAME/cpp-labpush access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed=> docker login / Docker Desktop login
- List all sections:
$ size ./build/cpp-lab- The expected output should be the following:
text data bss dec hex filename
14791 792 280 15863 ./build/cpp-lab.text: Text Segment - the executable code size, including: complied function, inline, template, constants, string literals.data: Initialized Data Segment - the memory for the global, static variables, has init value..bss: Uninitialized Data Segment - global and static variables that are not initialized