Debugging C++ CMake Projects
Debugging C++ / CMake Projects
When given a C++ project plus compiler output, do this first:
Bash# 1. Reproduce with warnings maxed out g++ -std=c++17 -Wall -Wextra -Wpedantic Main.cpp -o app # 2. Configure each CMake target independently cmake -S . -B build-tui cmake -S JNA-Desktop -B build-desktop cmake -S JNA-Mobile -B build-mobile -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake
Fix what the compiler actually reports before guessing at anything else.
Progress:
- [ ] Step 1: Clarify ambiguous requests only when a wrong guess is costly
- [ ] Step 2: Reproduce (compile/configure) and capture exact diagnostics
- [ ] Step 3: Triage into build errors, warnings, runtime bugs, layout problems
- [ ] Step 4: Fix smallest-first, one category at a time
- [ ] Step 5: Verify each target separately; state honestly what was NOT verified
- [ ] Step 6: Report what changed and what still needs the user's environment
Step 1: Resolve ambiguity quickly
Pick the most probable meaning and state the assumption, rather than stalling.
- "MS White" / "MS Wight" most likely means MSVC (Microsoft's compiler) or MSYS2. Default to MSVC if the project targets Windows desktop; MSYS2/MinGW if the log shows
g++/gcc. - If the user gets frustrated ("just do it"), stop asking questions and act on the best assumption.
Step 2: Reproduce
- Build every target on its own. A root
CMakeLists.txtthat only buildsMain.cppdoes not cover Qt targets. - Keep the first error. Later errors are usually cascades.
Step 3: Triage
| Category | Typical signs | First move |
|---|---|---|
| Build error | undefined reference, No such file | Check target_link_libraries, include paths, find_package |
| Warning | -Wignored-attributes etc. | Fix the type, don't silence blindly |
| Runtime logic | wrong output, lost data | Trace state changes and persistence |
| Layout | one CMake for three apps | Split per-target CMake files |
Step 4: Fix patterns
Pattern A: -Wignored-attributes on unique_ptr<FILE, decltype(&pclose)>
decltype(&PCLOSE) carries platform calling-convention attributes. Use a custom deleter type:
C++struct PipeCloser { void operator()(FILE* f) const { if (f) PCLOSE(f); } }; std::unique_ptr<FILE, PipeCloser> pipe(POPEN(cmd, "r"));
Pattern B: Prefix-matching commands
if (input.find("clear") == 0) also matches clearance. Require an exact match after trimming:
C++auto trim = [](std::string s){ s.erase(0, s.find_first_not_of(" \t\r\n")); s.erase(s.find_last_not_of(" \t\r\n") + 1); return s; }; if (trim(input) == "clear") { /* ... */ }
Pattern C: State cleared but not persisted
After any mutation (clear, delete, edit) call the save routine. Verify by reloading from disk.
Pattern D: Demo/sandbox mode overwriting real data
In non-interactive environments (no TTY), detect with isatty(STDIN_FILENO) and skip writes to the user's saved files.
Pattern E: Splitting UTF-8 when truncating
Never cut at a raw byte offset. Back up while (c & 0xC0) == 0x80:
C++size_t cut = std::min(max, s.size()); while (cut > 0 && (static_cast<unsigned char>(s[cut]) & 0xC0) == 0x80) --cut; return s.substr(0, cut);
Pattern F: Terminal UI not appearing
Flush after each frame: std::cout << frame << std::flush;. Without it, output is buffered and the screen looks frozen.
Pattern G: Qt CMake
CMAKEcmake_minimum_required(VERSION 3.21) project(JNA-Desktop LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_AUTOMOC ON) find_package(Qt6 REQUIRED COMPONENTS Quick Qml) qt_add_executable(JNA-Desktop Main.cpp AppStore.cpp JHClient.cpp Terminal.cpp) qt_add_qml_module(JNA-Desktop URI JNA VERSION 1.0 QML_FILES Main.qml) target_link_libraries(JNA-Desktop PRIVATE Qt6::Quick Qt6::Qml)
- Load QML via the module URI (
qrc:/JNA/Main.qml), not a relative filesystem path. - Mobile adds Android properties (
QT_ANDROID_PACKAGE_SOURCE_DIRpointing atandroid/).
Step 5: Verify honestly
If you cannot build Qt or Android in the current environment, say so. Never claim "tested" when only the plain C++ target was compiled.
Example 1: Online compiler warning
Input: warning: ignoring attributes on template argument 'int (*)(FILE*)' at unique_ptr<FILE,decltype(&PCLOSE)>
Output: Replace the decltype(&PCLOSE) deleter with a PipeCloser functor (Pattern A); recompile with -Wall -Wextra and confirm zero warnings.
Example 2: "Make a separate CMake for mobile"
Input: Repo has root CMakeLists.txt, JNA-Desktop/, JNA-Mobile/.
Output: Root builds only the Qt-free TUI (Main.cpp). JNA-Desktop/CMakeLists.txt and JNA-Mobile/CMakeLists.txt each declare their own project, Qt find_package, sources, and (mobile) Android manifest settings. Document the three configure commands in README.md.
Example 3: Frustrated user Input: "You keep saying you found 500 bugs, just fix it." Output: Skip preamble. List the exact files changed with one line each, run the available build, and clearly flag what could not be tested.
- Fix from compiler output, not intuition.
- Keep per-target CMake files independent; share code via a static library only if duplication grows.
- Pin
cmake_minimum_required(VERSION 3.21)andCMAKE_CXX_STANDARD 17to match the online compiler (GCC 13, C++17). - Guard platform code:
#ifdef _WIN32for_popen/_pclose, otherwisepopen/pclose. - Use
if(UNIX) target_link_libraries(... pthread)only where threads are used. - Keep the README's build section in sync with the actual CMake layout.
- Summarize in plain language: what broke, what changed, what is unverified.
- Claiming a Qt or Android build works without running it.
- Reporting vague counts of bugs ("fixed many issues") instead of specific changes.
- Using prefix matching or substring checks for exact commands.
- Mutating saved data inside demo or sandbox runs.
- Truncating strings by bytes when they contain UTF-8 box-drawing characters.
- Relying on a single root
CMakeLists.txtfor desktop, mobile, and TUI at once. - Asking repeated clarifying questions after the user has already signaled impatience.
- Forgetting
std::flushfor ANSI-redrawn terminal UIs.