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.
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.
9
u/v_maria Aug 07 '25
I had so much pain running cmake with emscripten
6
u/VomAdminEditiert Aug 07 '25
I'm working on my own Game engine and the installation/compilation is an absolute mess. This seems like a perfect match for me, thank you!
9
u/Over-Apricot- Aug 07 '25
I appreciate this 😭
Despite having built some sophisticated systems in major industries out there, its rather embarrassing to admit that cmake still baffles me 😭
3
u/mrexodia cmkr.build Aug 08 '25
The downsides of FetchContent are inaccurate:
You are confusing it with ExternalProject_Add. FetchContent only downloads at configure time, the targets are included in your project directly and only built at build time.
Practically true in most cases, but it’s possible to enable offline mode and pre-download the content.
You missed what I believe is the ideal way of managing dependencies: a superbuild project. This is where you use find_package to find dependencies, but provide a secondary project that uses ExternalProject_Add to build an independent (and pinned) prefix with all the dependencies installed. The CMAKE_PREFIX_PATH is then used to glue both projects together. This also allows advanced users/packagers to trivially use their system dependencies (which is idiotic for most commercial projects, but I digress).
Example superbuild project with LLVM and a bunch of other horrendous dependencies: https://github.com/LLVMParty/packages
There is no integration example public, but you basically can include the
packagesproject as a submodule and use some magic to automatically build it the first time (or tell the user how to).I plan to add superbuild/vendoring support in https://cmkr.build, but I first need to add proper packaging support (which also almost nobody knows how to do correctly, but I digress again).
2
u/CALL_420-360-1337 Aug 07 '25
This is so good!
7
u/Additional_Path2300 Aug 07 '25
You should be using out-of-tree builds instead of building within the source tree.
1
u/Mammoth_Age_2222 Aug 07 '25
Thanks for a really great article!
1
u/current_thread Aug 07 '25
Does the project support building with C++20 modules? Does it support vcpkg?
1
u/megikari Aug 07 '25
Awesome sharing
3
u/germandiago Aug 07 '25
I do not know who invented the syntax for generator expressions or made that mess with conditionals and that sh*tshow with escape sequences and function invocation but seriously... uh...
I use Meson for my projects but lately with the delay that there is for C++ modules support I am starting to consider CMake.
But I see those conditionals, those generator expressions, remember the variable caching, very "intuitive", those escape sequences when invoking scripts, that free-form cross-compilation mess and... well, I will stay with Meson for now.
All those, including subprojects, are solved well and I do not spend a minute doing stunts with installation and other stuff.
Just not worth for now, my project anyways is going to be mostly traditional file inclusion as of today.
For dependencies, I lean mostly on Conan.
2
u/SlowPokeInTexas Aug 07 '25
I have gone from hating CMake to simply disliking it but accepting its prevalence. ChatGPT helped a lot. I nevertheless thank you for this post, I shall refer to it in the future when I am pulling my two remaining hairs out.
1
u/dexter2011412 Aug 07 '25
Thank you for writing this up, I'll definitely take a look later. I was working on my own template project with modules but was too lazy to document it up. I had emscripten planned too lol
If you're not using payment in medium, it's better to either put this on your blog or somewhere else, because medium is actively ruining the experience for both the readers and the authors.
1
u/Nuxij Aug 07 '25
I will check this out, I had massive issues trying to get redis-plus-plus to see hiredis when I was using FetchContrnt, I reverted to just expecting it to be in the system path
1
u/Conscious-Secret-775 Aug 09 '25
Maybe I am missing something but you don't seem to be using a CMakePresets file? For any non-trivial CMake project, I don't know why someone wouldn't use presets. They are supported by both Visual Studio and CLion and perhaps Visual Studio Code too (I have no experience with that).
Also, for third party dependencies, I have found vcpkg to be the preferable tool. Unlike Conan, it integrates very well with CMake and it's easy to create your own vcpkg registry, it's just a git repo with some CMake and vcpkg config files.
1
u/kimkulling Aug 11 '25
Cool, I will have a look!
1
u/Specialist_Gur4690 Aug 11 '25
Have you heard of https://github.com/CarloWood/gitache ? This is a cmake util for downloading, configuring, building and caching other git repositories.
1
u/Total-Skirt8531 Aug 19 '25
Here's a random CMake question for anyone who feels like answering. Obviously i am currently researching this reading books, googling, etc.
i have a pretty specific cmake question.
i have a project where i have to build several versions of a shared object library, so i give it:
# create the libname target and give sources for building it and declare it shared
add_library(libname SHARED libname.cpp)
# tell it to install in a specific path
install( TARGETS libname LIBRARY DESTINATION "specific_path_name_to_directory" )
then in the application CMakeLists.txt where i'm trying to link to this library i give it these 3 lines:
# create an executable target with source file
add_executable( application_name source_file.cpp)
# add_subdirectory to build the library, which does the add_library command
# to create the library target
add_subdirectory( "absolute path to library source where the library's CMakeLists.txt file is" "${CMAKE_BINARY_DIR}/libraries/libname")
# tell the app to link to the library
target_link_libraries( application_name libname )
-------------
after building the application i look at what is linked:
ldd application_name
and i would expect to see my library linked from "specific_path_name_to_directory" but it's not, it's linked from the "lib" directory in my install path. that path in "specific_path_name_to_directory" does exist.
i'm hoping someone will know what i'm talking about and know what's going on.
thanks
1
u/cthutu Aug 23 '25
For all my C development, I've switched to a no-build system where the build script is C. Google tsoding and no-build to know what I'm talking about.
1
u/Appropriate-Tap7860 Sep 08 '25
Hey. do you know how to compile a cmake based library with ue5?
0
u/[deleted] Aug 07 '25
[deleted] — view deleted comment