r/cpp • u/PigeonCodeur • Aug 07 '25
I wrote a comprehensive guide to modern CMake using a real 80-file game engine project (not another hello-world tutorial)
After struggling with CMake on my game engine project for months, I decided to document everything I learned about building professional C++ build systems.
Most CMake tutorials show you toy examples with 2-3 files. This guide uses a complex project - my ColumbaEngine, an open source c++ game engine ([github](https://github.com/Gallasko/ColumbaEngine)) with 80+ source files, cross-platform support (Windows/Linux/Web), complex dependencies (SDL2, OpenGL, FreeType), and professional distribution.
Part 1 covers the compilation side:
- Modern target-based CMake (no more global variables!)
- Dependency management strategies (vendoring vs package managers vs FetchContent)
- Cross-platform builds including Emscripten for web
- Precompiled headers that cut build times by 60%
- Generator expressions and proper PUBLIC/PRIVATE scoping
- Testing infrastructure with GoogleTest
The examples are all from production code that actually works, not contrived demos.
Part 2 (coming soon) will cover installation, packaging, and distribution - the stuff most tutorials skip but is crucial for real projects.
Hope this helps other developers dealing with complex C++ builds! Happy to answer questions about specific CMake pain points.
View all comments
41
u/not_a_novel_account cmake dev Aug 07 '25
Mostly good. Fast stuff because I use reddit too much at work already:
CMAKE_variables. You mention this at the end but violate it at the beginning. It's not up to you what C++ version I build your code with, etc. Maybe I need all the code compiled with C++23, maybe C++26, maybe C++41. Your project doesn't know, so don't make the assumption. Leave globals alone, I setup them up just how I like them. If you need guaranteed features usetarget_compile_features().add_executable()is weird and can possibly lead to some unexpected behavior when done withadd_library. It's not wrong exactly, just strange, put them all intarget_sources().target_include_directories(), preferFILE_SET, the obscure generator expressions and complex install commands thattarget_include_directories()will necessitate are precisely why. This is also why CMake 3.22 is a little too old to be considered "modern".if().FetchContent, etc, are hostile to packagers, and require downstream patching to be removed. These should always be behind default-off options. The default build configuration should assume thatfind_package()works because the packager producing the build has correctly configured the build environment. Modern CMake isfind_package(),FetchContentis a mechanism for super-builds, not individual projects.Generally go check out the Beman Standard for CMake. Their best practices are the upstream recommended best practices and they have plenty of projects exercising them.