Skip to content

mhammer3/cpp_gtest_coverage_demo

Repository files navigation

--

# ✅ **README — GoogleTest + CMake + MinGW-w64 (UCRT64) + Coverage Setup**

This project demonstrates how to set up a complete C/C++ development environment on Windows using:

* **MSYS2 UCRT64**
* **MinGW-w64**
* **CMake**
* **GoogleTest**
* **gcovr**

It includes building, testing, and generating a code coverage report.

---

# ✅ 1. Prerequisites

* **Windows 10 or 11**
* **MSYS2** → [https://www.msys2.org](https://www.msys2.org)
* **MSYS2 UCRT64 Shell** (**important!**)

---

# ✅ 2. Install and Update MSYS2

1. Download MSYS2:
   [https://www.msys2.org](https://www.msys2.org)

2. Start **MSYS2 MSYS Shell** (not MinGW yet)

3. Update packages:

```bash
pacman -Syuu
```

⚠️ The shell may close during update. Restart it and repeat the command until no updates remain.

---

# ✅ 3. Use the *Correct* Shell (UCRT64!)

Start:

### ✅ **MSYS2 UCRT64**

Your prompt should look like:

```
user@PC UCRT64 ~
```

### ❌ Do NOT use:

* MSYS
* MinGW32
* MinGW64
* CLANG64

Only **UCRT64** ensures:

* full C++17 compatibility
* working GoogleTest builds

---

# ✅ 4. Install Compiler, CMake, and Build Tools

Install the required UCRT64 packages:

```bash
pacman -S mingw-w64-ucrt-x86_64-gcc
pacman -S mingw-w64-ucrt-x86_64-cmake
pacman -S mingw-w64-ucrt-x86_64-make
```

### Verify installation:

```bash
which g++
which cmake
```

Both must point to:

```
/ucrt64/bin/...
```

---

# ✅ 5. Using GoogleTest Locally

This project includes GoogleTest directly:

```
googletest/
├── googletest/
│   ├── include/
│   └── src/
└── googlemock/
    ├── include/
    └── src/
```

The source originates from:

```
googletest-release-1.17.0.zip
```

### Important:

* ❌ Do NOT install GoogleTest via `pacman`
* ❌ Do NOT use `FetchContent`
* ✅ This project builds GoogleTest locally

---

# ✅ 6. Install Coverage Tool (gcovr)

Install:

```bash
pacman -S mingw-w64-ucrt-x86_64-gcovr
```

Verify:

```bash
gcovr --version
```

---

# ✅ 7. Project Structure

Clone the repository:

```bash
git clone --recurse-submodules https://github.com/mhammer3/cpp_gtest_coverage_demo.git
```

Directory layout:

```
cpp_gtest_coverage_demo/
│
├── src/                  # C source code
├── tests/                # C++ GoogleTest files
├── googletest/           # Local GoogleTest / GoogleMock source
│
├── build/                # Generated during build
│
├── all.sh                # Build + test + coverage
├── build.sh              # Build only
├── run_tests.sh          # Run tests only
├── coverage.sh           # Coverage only
│
└── CMakeLists.txt
```

---

# ✅ 8. Build and Run

Run everything:

```bash
./all.sh
```

Or step-by-step:

```bash
./build.sh
./run_tests.sh
./coverage.sh
```

---

# ✅ 9. Open Coverage Report

```bash
explorer.exe coverage/coverage.html
```

# ✅ 10. Clang Tidy Installation
```bash
pacman -S mingw-w64-ucrt-x86_64-clang \
          mingw-w64-ucrt-x86_64-clang-tools-extra
```
---

# ✅ 11. Analyze with Clang Tidy 
```bash
./analyze_clang_tidy.sh
```

or 
```bash
./all.sh
```

---

# ✅ Done!

---

# 🧪 Debugging in Visual Studio Code

This setup allows you to:

* ✅ run unit tests (`unit_tests.exe`) from VSCode
* ✅ set breakpoints
* ✅ debug step-by-step through C and C++ code
* ✅ use **MSYS2 UCRT64 + MinGW-w64**

All without:

* PATH issues
* GDB errors
* MSVC dependencies

---

# ✅ 1. Prerequisites (Debugging)

Install in **MSYS2 UCRT64 shell**:

```bash
pacman -S mingw-w64-ucrt-x86_64-gcc
pacman -S mingw-w64-ucrt-x86_64-gdb
pacman -S mingw-w64-ucrt-x86_64-cmake
```

### Verify GDB:

```bash
which gdb
```

Expected:

```
/ucrt64/bin/gdb   ✅ correct
/usr/bin/gdb      ❌ wrong
```

---

# ✅ 2. Install VSCode Extensions

In Visual Studio Code:

### Required:

* **C/C++ Extension** (`ms-vscode.cpptools`)
* **CMake Tools** (`ms-vscode.cmake-tools`)

### Optional:

* Test Explorer UI (for test discovery)

---

# ✅ 3. Open Project in VSCode

```bash
code .
```

or open via Explorer.

---

# ✅ 4. Configure VSCode for MinGW-w64

Create folder:

```
.vscode/
```

---

# ✅ 5. File: `.vscode/c_cpp_properties.json`

```json
{
    "configurations": [
        {
            "name": "MinGW UCRT64",
            "includePath": [
                "${workspaceFolder}/src",
                "${workspaceFolder}/googletest/googletest/include",
                "${workspaceFolder}/googletest/googlemock/include"
            ],
            "defines": [],
            "compilerPath": "C:/msys64/ucrt64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}
```

### Important:

* ✅ Must point to `ucrt64/bin/g++.exe`
* ❌ Do NOT use MinGW32

---

# ✅ 6. File: `.vscode/launch.json` (Debug Tests)

```json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug unit_tests.exe",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/unit_tests.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/build",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/msys64/ucrt64/bin/gdb.exe",
            "setupCommands": [
                { "text": "-gdb-set breakpoint pending on" }
            ]
        }
    ]
}
```

### Important:

* ✅ `program` → `unit_tests.exe`
* ✅ `miDebuggerPath` → UCRT64 GDB
* ✅ Supports breakpoints in C and C++

---

# ✅ 7. File: `.vscode/tasks.json` (Build)

```json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "CMake Build",
            "type": "shell",
            "command": "cmake",
            "args": [
                "--build", "build"
            ],
            "group": "build",
            "problemMatcher": []
        }
    ]
}
```

---

# ✅ 8. Build via VSCode (CMake Tools)

Use the CMake Tools status bar:

* ✅ Select Kit → **GCC 64-bit (ucrt64)**
* ✅ Click **Configure**
* ✅ Click **Build**

This will generate:

```
unit_tests.exe
```

---

# ✅ 9. Debug Tests
 Select the bug symbol at the bottom of the VS Code window

About

Lightweight C/C++ test framework demo for Embedded and Desktop projects using GoogleTest, GoogleMock, CMake, MSYS2 UCRT64, coverage reporting, static analysis, and VS Code integration.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors