As a warm-up to my CMake series, I wanted to answer a few questions I struggled with but had a hard time finding a good answer for.
Q1. What is cmake? I mean… really?
It drives me crazy when someone answers a question such a “what is?” with a wikipedia like entry, so let’s give this a shot.
Have you compiled your code yet? When I do it, it usually looks something like:
g++ my_achy_breaky_heart.cpp -o billy_ray
This will take the C++ file
my_achy_breaky_heart.cpp and create a file that you can run called
billy_ray. This is simple and easy to do. But what about the rest of your favorite Billy Ray Cyrus songs, you ask? Let’s add just a few of those:
g++ my_achy_breaky_heart.cpp words_by_heart.cpp somebody_new.cpp -o billy_ray
Phew ok. So that’s a bit annoying. Really? I have to type all that in. I’ll just use a makefile. All is well and good and eventually you turn
billy_ray into a library and create another library for
miley_cyrus. Eventually, your friend who uses Windows wants to use your fantastic libraries. You send him the code and he proclaims your code is junk! He can’t get your makefile to output anything useful. Oops! You made the makefile assuming your make file would work on Windows just like it did on you Mac OS.
Well, that’s where CMake comes in. CMake, at least for our beginner purposes, generates different makefiles for different operating systems. Instead of running something like:
Now you’ll do:
It’s a little longer, but the benefits are huge! CMake generates the correct build files for whatever operating system you need. (And it can do more than just that, but let’s stay grounded in the simple.)
Q2. How big should my project be before I use CMake?
I believe that just as you should
git init all of your projects from the start, I believe you should also CMake all of your C/C++ projects right from the get go. The difficulty of CMake commands for x project are proportional to the size of x, which means small project = easy to make CMakeLists.txt. Here is an example of the above
# Not required, but nice to put.
cmake_minimum_required ( VERSION 2.8 FATAL_ERROR )
project ( best_of_billy_ray )
add_executable ( billy_ray
That’s it. And if you look at how many lines are actually required, it’s just one! (I broke the
add_executable line so it would fit on small screens)
There are many small projects that say something like “this project is too simple to use a full blown build system”. When I encounter that, I go a little crazy. CMake is a full blown build system, but it isn’t a full blown head ache for small projects, and if you include a CMake from the start, it’ll make your project easier to use, cross-platform, and easier to integrate with! By golly, I say use CMake from the start.
More to come