Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
*build
*private*
*.vscode
.vscode/
*Identifier
*Testing*
*Testing*
coverage_gcovr
coverage_lcov
!.vscode/launch.json
!.vscode/tasks.json
.cache
30 changes: 30 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/debug/bin/cpp_lab_project", // path to the executable
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}", // working directory
"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
}
],
},
]
}
34 changes: 34 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"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"
}
]
}
165 changes: 152 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
## 1. Overview
**TL;DR**
```bash
./r
./scripts/run.sh
```

**Project Structure**
**Project Hierarchy**
```
includes/ → Header files (.h, .hpp)
src/ → Source files (.cpp)
tests/ → GoogleTest test cases
```

## 2. Dependencies
Make sure the following tools are installed before building the project:
- **g++ / gcc**
Expand All @@ -36,40 +37,44 @@ Get-ChildItem -Recurse -Include *.cpp, *.h, *.hpp | ForEach-Object { clang-forma
### 3.1. Setup the Local Test Environment
- **Ubuntu system**
* Install `gcc`, `cmake`, `git`, and `pthread` (Skip this step if you already install)
```
```bash
$ 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
```
```bash
$ cd build
$ cmake ..
$ cmake --build .
```
* Run the application and the test
```
$ ./cpp_lab_project
$ ./cpp_lab_project_test
```bash
$ ./bin/cpp_lab_project
$ ./bin/cpp_lab_project_test
```
* Detect Memory Leak Using [valgrind](https://valgrind.org/)
```
$ sudo apt install valgrind
```bash
$ valgrind --leak-check=full -v ./cpp-lab
```
* (Optional) Run static analysis - cppcheck
```
$ sudo apt-get install cppcheck
```bash
$ cppcheck "folder" / "file"
```
* (Optional) Run static analysis - clang-tidy
```
$ sudo apt-get install -y clang-tidy
```bash
$ clang-tidy -p build -header-filter='^src/.*' $(find src -name "*.cpp")
```
* (Optional) Run coverage - lcov
```bash
$ ./scripts/gen_coverage_lcov.sh
```
- **Docker**
* Update `Dockerfile`
* Build the Docker image
Expand All @@ -95,6 +100,140 @@ Get-ChildItem -Recurse -Include *.cpp, *.h, *.hpp | ForEach-Object { clang-forma
* `cpp-lab:latest`: the image you built earlier.
* `/bin/bash`: the command to execute inside the container (opens a Bash shell).

### 3.2 Config C/C++ Debugging (VS Code)

#### 3.2.1. Launch Configuration

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.
##### a. Create `launch.json`
- Go to `Run` → `Add Configuration...` or
- Use VS Code chat command:
```
/startDebugging
```
to auto-generate a debug configuration.

##### b. Start Debugging

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`

#### 3.2.2. Debug Configuration Example

The most important field is the executable that will run:

```json
// .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
}
]
}
]
}
```

#### 3.2.3. Build Tasks (CMake)

An example `tasks.json` for CMake builds:

```json
// .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"
}
]
}
```
#### 3.2.4. Run Steps

1. **Open** your project in VS Code
2. **Install Extension:**
- *C/C++ Extension Pack*
3. **Install GDB:**
```bash
sudo apt update
sudo apt install gdb
```
4. **Start Debugging:**
Press `F5`
5. **Run Without Debugging:**
Press `Ctrl + F5`
6. **Build Project (Optional):**
Press `Ctrl + Shift + B`

Notes:
| Shortcut | Action |
|-----------------|-----------------------|
| F5 | Start debugging |
| Ctrl + F5 | Run without debugging |
| Ctrl + Shift + D| Open Debug panel |
| Ctrl + Shift + B| Build project |

### 3.3 Documentation with `doxygen`
TBD - Refer to this [Documentation with doxygen](https://www.labri.fr/perso/fleury/posts/programming/using-cmake-googletests-and-gcovr-in-a-c-project.html#:~:text=of%20the%C2%A0project.-,Documentation%20with%20doxygen,-Code%20embedded%20documentation)

## 5. Update Docker Image
```bash
# Navigate to the project that contain your Dockerfile
Expand Down
46 changes: 46 additions & 0 deletions scripts/gen_coverage_gcovr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
set -e

# -----------------------------------------------------------------------------
# Check gcovr installation
# -----------------------------------------------------------------------------
if ! command -v gcovr >/dev/null 2>&1; then
echo "Error: gcovr is not installed."
echo "Install with: pip install gcovr or sudo apt install gcovr or sudo apt install python3-gcovr"
exit 1
fi

# -----------------------------------------------------------------------------
# Configure project (only if build folder does not exist)
# -----------------------------------------------------------------------------
if [ ! -d build ]; then
cmake -S . -B build \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="--coverage -O0 -g"
fi

# -----------------------------------------------------------------------------
# Build project
# -----------------------------------------------------------------------------
cmake --build build

# -----------------------------------------------------------------------------
# Run unit tests
# -----------------------------------------------------------------------------
ctest --test-dir build --output-on-failure

# -----------------------------------------------------------------------------
# Generate coverage report
# -----------------------------------------------------------------------------
mkdir -p coverage_gcovr

gcovr -r . build \
--branches \
--html \
--html-details \
-o coverage_gcovr/index.html

# -----------------------------------------------------------------------------
# Open coverage report
# -----------------------------------------------------------------------------
xdg-open coverage_gcovr/index.html
Loading
Loading