Skip to content

Commit db35477

Browse files
authored
Merge pull request #48 from urbytes21/dev_branch_3
id 1773284082
2 parents 12b0c70 + 42c1965 commit db35477

24 files changed

+1366
-54
lines changed

.gitignore

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
*build
22
*private*
3-
*.vscode
3+
.vscode/
44
*Identifier
5-
*Testing*
5+
*Testing*
6+
coverage_gcovr
7+
coverage_lcov
8+
!.vscode/launch.json
9+
!.vscode/tasks.json
10+
.cache

.vscode/launch.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "(gdb) Launch",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"program": "${workspaceFolder}/build/debug/bin/cpp_lab_project", // path to the executable
9+
"args": [],
10+
"stopAtEntry": true,
11+
"cwd": "${workspaceFolder}", // working directory
12+
"environment": [],
13+
"externalConsole": false,
14+
"MIMode": "gdb",
15+
"preLaunchTask": "CMake Debug Build",
16+
"setupCommands": [
17+
{
18+
"description": "Enable pretty-printing for gdb",
19+
"text": "-enable-pretty-printing",
20+
"ignoreFailures": true
21+
},
22+
{
23+
"description": "Set Disassembly Flavor to Intel",
24+
"text": "-gdb-set disassembly-flavor intel",
25+
"ignoreFailures": true
26+
}
27+
],
28+
},
29+
]
30+
}

.vscode/tasks.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "CMake Debug Configure",
6+
"type": "shell",
7+
"command": "cmake",
8+
"args": [
9+
"-S",
10+
".",
11+
"-B",
12+
"build/debug",
13+
"-DCMAKE_BUILD_TYPE=Debug"
14+
]
15+
},
16+
{
17+
"label": "CMake Debug Build",
18+
"type": "shell",
19+
"command": "cmake",
20+
"args": [
21+
"--build",
22+
"build/debug"
23+
],
24+
"group": {
25+
"kind": "build",
26+
"isDefault": true
27+
},
28+
"presentation": {
29+
"reveal": "always"
30+
},
31+
"dependsOn": "CMake Debug Configure"
32+
}
33+
]
34+
}

README.md

Lines changed: 152 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
## 1. Overview
22
**TL;DR**
33
```bash
4-
./r
4+
./scripts/run.sh
55
```
66

7-
**Project Structure**
7+
**Project Hierarchy**
88
```
99
includes/ → Header files (.h, .hpp)
1010
src/ → Source files (.cpp)
1111
tests/ → GoogleTest test cases
1212
```
13+
1314
## 2. Dependencies
1415
Make sure the following tools are installed before building the project:
1516
- **g++ / gcc**
@@ -36,40 +37,44 @@ Get-ChildItem -Recurse -Include *.cpp, *.h, *.hpp | ForEach-Object { clang-forma
3637
### 3.1. Setup the Local Test Environment
3738
- **Ubuntu system**
3839
* Install `gcc`, `cmake`, `git`, and `pthread` (Skip this step if you already install)
39-
```
40+
```bash
4041
$ sudo apt-get update
4142
$ sudo apt-get install g++
4243
$ sudo apt-get install lcov
4344
$ sudo apt-get install cmake
4445
$ sudo apt-get install git
46+
$ sudo apt install valgrind
4547
$ sudo apt-get install cppcheck
48+
$ sudo apt-get install -y clang-tidy
49+
$ sudo apt install python3-gcovr
4650
```
4751
* Build the application and the tests
48-
```
52+
```bash
4953
$ cd build
5054
$ cmake ..
5155
$ cmake --build .
5256
```
5357
* Run the application and the test
54-
```
55-
$ ./cpp_lab_project
56-
$ ./cpp_lab_project_test
58+
```bash
59+
$ ./bin/cpp_lab_project
60+
$ ./bin/cpp_lab_project_test
5761
```
5862
* Detect Memory Leak Using [valgrind](https://valgrind.org/)
59-
```
60-
$ sudo apt install valgrind
63+
```bash
6164
$ valgrind --leak-check=full -v ./cpp-lab
6265
```
6366
* (Optional) Run static analysis - cppcheck
64-
```
65-
$ sudo apt-get install cppcheck
67+
```bash
6668
$ cppcheck "folder" / "file"
6769
```
6870
* (Optional) Run static analysis - clang-tidy
69-
```
70-
$ sudo apt-get install -y clang-tidy
71+
```bash
7172
$ clang-tidy -p build -header-filter='^src/.*' $(find src -name "*.cpp")
7273
```
74+
* (Optional) Run coverage - lcov
75+
```bash
76+
$ ./scripts/gen_coverage_lcov.sh
77+
```
7378
- **Docker**
7479
* Update `Dockerfile`
7580
* Build the Docker image
@@ -95,6 +100,140 @@ Get-ChildItem -Recurse -Include *.cpp, *.h, *.hpp | ForEach-Object { clang-forma
95100
* `cpp-lab:latest`: the image you built earlier.
96101
* `/bin/bash`: the command to execute inside the container (opens a Bash shell).
97102
103+
### 3.2 Config C/C++ Debugging (VS Code)
104+
105+
#### 3.2.1. Launch Configuration
106+
107+
To debug C/C++ projects in VS Code, you must create a *launch configuration* that specifies:
108+
109+
- The application entry point (executable)
110+
- How to attach to a running process
111+
- Environment variables needed
112+
- Saved debugging setup details
113+
114+
All launch configs are stored in `.vscode/launch.json` within your workspace.
115+
##### a. Create `launch.json`
116+
- Go to `Run``Add Configuration...` or
117+
- Use VS Code chat command:
118+
```
119+
/startDebugging
120+
```
121+
to auto-generate a debug configuration.
122+
123+
##### b. Start Debugging
124+
125+
You can start a debug session in several ways:
126+
- **F5**
127+
- **Ctrl + Shift + D**: Open Debug panel, select a config, press Start
128+
- **Ctrl + Shift + P**select `Debug: Select and Start Debugging`
129+
130+
#### 3.2.2. Debug Configuration Example
131+
132+
The most important field is the executable that will run:
133+
134+
```json
135+
// .vscode/launch.json
136+
{
137+
"version": "0.2.0",
138+
"configurations": [
139+
{
140+
"name": "(gdb) Launch",
141+
"type": "cppdbg",
142+
"request": "launch",
143+
"program": "${workspaceFolder}/build/debug/bin/cpp_lab_project",
144+
"args": [],
145+
"stopAtEntry": true,
146+
"cwd": "${workspaceFolder}",
147+
"environment": [],
148+
"externalConsole": false,
149+
"MIMode": "gdb",
150+
"preLaunchTask": "CMake Debug Build",
151+
"setupCommands": [
152+
{
153+
"description": "Enable pretty printing for gdb",
154+
"text": "-enable-pretty-printing",
155+
"ignoreFailures": true
156+
},
157+
{
158+
"description": "Set disassembly flavor to Intel",
159+
"text": "-gdb-set disassembly-flavor intel",
160+
"ignoreFailures": true
161+
}
162+
]
163+
}
164+
]
165+
}
166+
```
167+
168+
#### 3.2.3. Build Tasks (CMake)
169+
170+
An example `tasks.json` for CMake builds:
171+
172+
```json
173+
// .vscode/tasks.json
174+
{
175+
"version": "2.0.0",
176+
"tasks": [
177+
{
178+
"label": "CMake Debug Configure",
179+
"type": "shell",
180+
"command": "cmake",
181+
"args": [
182+
"-S",
183+
".",
184+
"-B",
185+
"build/debug",
186+
"-DCMAKE_BUILD_TYPE=Debug"
187+
]
188+
},
189+
{
190+
"label": "CMake Debug Build",
191+
"type": "shell",
192+
"command": "cmake",
193+
"args": [
194+
"--build",
195+
"build/debug"
196+
],
197+
"group": {
198+
"kind": "build",
199+
"isDefault": true
200+
},
201+
"presentation": {
202+
"reveal": "always"
203+
},
204+
"dependsOn": "CMake Debug Configure"
205+
}
206+
]
207+
}
208+
```
209+
#### 3.2.4. Run Steps
210+
211+
1. **Open** your project in VS Code
212+
2. **Install Extension:**
213+
- *C/C++ Extension Pack*
214+
3. **Install GDB:**
215+
```bash
216+
sudo apt update
217+
sudo apt install gdb
218+
```
219+
4. **Start Debugging:**
220+
Press `F5`
221+
5. **Run Without Debugging:**
222+
Press `Ctrl + F5`
223+
6. **Build Project (Optional):**
224+
Press `Ctrl + Shift + B`
225+
226+
Notes:
227+
| Shortcut | Action |
228+
|-----------------|-----------------------|
229+
| F5 | Start debugging |
230+
| Ctrl + F5 | Run without debugging |
231+
| Ctrl + Shift + D| Open Debug panel |
232+
| Ctrl + Shift + B| Build project |
233+
234+
### 3.3 Documentation with `doxygen`
235+
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)
236+
98237
## 5. Update Docker Image
99238
```bash
100239
# Navigate to the project that contain your Dockerfile

scripts/gen_coverage_gcovr.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# -----------------------------------------------------------------------------
5+
# Check gcovr installation
6+
# -----------------------------------------------------------------------------
7+
if ! command -v gcovr >/dev/null 2>&1; then
8+
echo "Error: gcovr is not installed."
9+
echo "Install with: pip install gcovr or sudo apt install gcovr or sudo apt install python3-gcovr"
10+
exit 1
11+
fi
12+
13+
# -----------------------------------------------------------------------------
14+
# Configure project (only if build folder does not exist)
15+
# -----------------------------------------------------------------------------
16+
if [ ! -d build ]; then
17+
cmake -S . -B build \
18+
-DCMAKE_BUILD_TYPE=Debug \
19+
-DCMAKE_CXX_FLAGS="--coverage -O0 -g"
20+
fi
21+
22+
# -----------------------------------------------------------------------------
23+
# Build project
24+
# -----------------------------------------------------------------------------
25+
cmake --build build
26+
27+
# -----------------------------------------------------------------------------
28+
# Run unit tests
29+
# -----------------------------------------------------------------------------
30+
ctest --test-dir build --output-on-failure
31+
32+
# -----------------------------------------------------------------------------
33+
# Generate coverage report
34+
# -----------------------------------------------------------------------------
35+
mkdir -p coverage_gcovr
36+
37+
gcovr -r . build \
38+
--branches \
39+
--html \
40+
--html-details \
41+
-o coverage_gcovr/index.html
42+
43+
# -----------------------------------------------------------------------------
44+
# Open coverage report
45+
# -----------------------------------------------------------------------------
46+
xdg-open coverage_gcovr/index.html

0 commit comments

Comments
 (0)