Learning to embrace CMake instead of hate it
I used to avoid CMake like the plague, and hated how everyone else seemed to use it for their C++ projects (it's the de-facto standard for C++). I know I'm not alone, because there's plenty of anti-CMake hate online.
When I first encountered CMake, I found the syntax to be cryptic, the documentation unhelpful, and the error messages unintelligible. Build errors were basically "game over" for me, because I had no idea how to fix them.
Makefiles looked so much better than CMakelists
My opinion was that good old makefiles were so much better. It wasn't true, objectively. To a newcomer makefiles also look cryptic, and it takes time to learn. Nevertheless, makefiles made sense to me at the time, and CMake did not.
Box2D made me do it
Fast forward to December 2022. I wanted to use Box2D as physics engine in my Raylib 2D Challenge. Box2D is written in C++ and, you guessed it, uses CMake as build system. There was no generic makefile as backup (like some projects do).
In the past I worked around this by manually writing makefiles, but I really didn't want to do that this time. So, I decided to go the other way, and convert my code's makefile to CMake. It was time to give CMake another shot.
Modern CMake is a lot better
I was pleasantly surprised at how much better CMake was than my previous experience years ago. Creating the CMakelists.txt file was relatively easy, and both the documentation and error messages have improved greatly. The CMake development team have certainly done a lot of work.
Of course, my ability to understand it all has also improved over the years, and that no doubt helped too...
My new CMakelists file is less than one quarter the length of the original makefile, which was a welcome surprise. You can compare them yourself by looking at the Makefile in this archive, with the CMakelists.txt file in this archive.
CMake still has it's problems
Of course, CMake is far from perfect. Nothing this complex is going to suit everyone, and there will always be things people would like it to do that it can't, or that feel needlessly difficult.
One problem is that, in true C++ style, CMake has massive backward compatibility. It needs to, or there'll be hordes of angry developers whose CMake scripts stopped working with the last update. While backward compatibility sounds good, it means that there are heaps of tutorials and examples teaching old nasty ways of doing things. So, you can end up learning the old stuff, which you then have to unlearn in order to do it the right way.
Getting started with modern CMake
Here are a few links that should help shorten your CMake learning curve:
- Learn CMake by Example (my contribution to help cut through the mess)
- Introduction to modern CMake for beginners
- CMake cheat-sheet (WARNING: It's free, but you need to enter email address to get the cheat-sheet)
- CMake documentation
- CMake's official forum
- CMake on reddit (better for asking questions)
Post your comment