Update: Using C++26 with contracts enabled

This commit is contained in:
David Gil de Gómez Pérez
2026-08-12 13:47:50 +03:00
parent 206742e9b3
commit 41111dba30
6 changed files with 125 additions and 24 deletions
+52 -16
View File
@@ -17,37 +17,40 @@ brew install gcc@16
## Quick Start (CLion)
1. Open this folder in CLion (`File → Open…`).
2. Use the **`default`** CMake profile (`GCC 16 + C++26` from `CMakePresets.json`).
3. Set the toolchain to **GCC 16** (`/opt/homebrew/bin/g++-16`).
4. **Disable Clang-based analysis** (this project targets GCC only):
- **Settings → Languages & Frameworks → C/C++ → Clangd** → disable, **or**
- **Settings → Editor → Inspections → C/C++ → General → Clang-Tidy** → disable
5. Reload CMake, pick an exercise target (e.g. `02_control_flow_loops`), implement TODOs, run tests.
1. Open this folder in CLion.
2. **Settings → Build, Execution, Deployment → CMake**
3. Enable profile **`default`** (display name **GCC 16 + C++26** from `CMakePresets.json`).
4. Disable old profiles that use Apple Clang (e.g. custom profiles without the preset toolchain).
5. Click **Reload CMake Project**.
If configure fails with *"This project requires GCC 16+"*, CLion is still using Apple Clang. Enable the **`default`** preset or set the toolchain C++ compiler to `g++-16`.
If configure fails with *"generator does not match"*, delete the build folder (`cmake-build-debug-gcc-16-cpp26`) and reload CMake.
6. Pick an exercise target (e.g. `02_control_flow_loops`) and run tests.
## Quick Start (Terminal)
```bash
cmake --preset default
cmake --build build
ctest --test-dir build --output-on-failure
cmake --build cmake-build-debug-gcc-16-cpp26
ctest --test-dir cmake-build-debug-gcc-16-cpp26 --output-on-failure
```
Run a single exercise:
```bash
cmake --build build --target 02_control_flow_loops
./build/modules/02_control_flow/02_control_flow_loops
cmake --build cmake-build-debug-gcc-16-cpp26 --target 02_control_flow_loops
./cmake-build-debug-gcc-16-cpp26/modules/02_control_flow/02_control_flow_loops
```
Manual configure (without presets):
```bash
cmake -S . -B build \
-DCMAKE_CXX_COMPILER=/opt/homebrew/bin/g++-16 \
-DCMAKE_C_COMPILER=/opt/homebrew/bin/gcc-16 \
cmake -S . -B cmake-build-debug-gcc-16-cpp26 \
-DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/gcc-16.cmake \
-DCOURSE_ENABLE_CONTRACTS=ON
cmake --build build
cmake --build cmake-build-debug-gcc-16-cpp26
```
## Project Structure
@@ -143,10 +146,43 @@ auto factorial(const int n) -> long long
- Use the **GCC 16** toolchain, not Apple Clang.
- Disable **Clangd** and **Clang-Tidy** inspections — they use the Clang frontend and do not support GCC C++26 contracts.
- Use CLions built-in **GCC** parser/inspections, or rely on compiler errors from the build.
- **Cppcheck plugin:** disable inline analysis for this project (see [Cppcheck](#cppcheck) below).
- Use **Run | Run…** and filter by module prefix (e.g. `02_control_flow`).
- Enable **Google Test** integration to see individual tests in the tree view.
## Cppcheck
Cppcheck **does not understand GCC C++26 contracts** (`pre(...)`) or GCC 16 libstdc++ when the CLion plugin analyzes headers in isolation. That produces false IDE errors such as:
- `Include file: <contracts> not found`
- `failed to evaluate #elif condition` in libstdc++ headers
- parse errors on `pre(...)`
### Recommended: disable the plugin, use the CMake target
1. **Settings → Editor → Inspections → C/C++ → Cppcheck****disable**
2. Configure and build once:
```bash
cmake --preset default
cmake --build build
```
3. Run Cppcheck from CLion or terminal:
```bash
cmake --build build --target cppcheck_modules
```
This uses `compile_commands.json` from your **GCC 16** build and suppresses known false positives (`cppcheck/suppressions.txt`).
### If you keep the plugin anyway
**Settings → Cppcheck configuration → Options:**
```
--language=c++ --std=c++26 --max-configs=1 --suppress=missingIncludeSystem --suppress=syntaxError
```
Expect remaining false positives on contract syntax. The plugin is not a good fit for this codebase.
## Notes on C++26
All exercises build with **C++26**. Module READMEs describe the language feature each exercise teaches (C++11, C++17, etc.), but the toolchain baseline is C++26 throughout.