CMake is a wonderful build system for C/C++ but it can be hard to learn. The documentation is good yet technical and there aren’t a plethora of layman tutorials. I want to help make a dent in that problem. I find that my projects are more powerful now that I use CMaek. I’m also a CMake beginner so hopeful we can both get something out of this series.

I will state what CMake version I’m using at the beginning of each tutorial, example projects that relate to the topic, my code on github, and references to any relevant documentation. With all that said, here’s the overview!


I’m going to write this series out of order since what I need day to day changes unpredictably. I will keep reorganizing as I go along so eventually it will all weave together nicely. Here is an (incomplete and bound to change) overview of the topics I will cover:

  • Warm Up I: CMake My FAQs
  • Warm Up II: CMake In Action
  • Part I: CMake Variable Scope
  • Part II: CMake Super Project
  • Part III: CMake Variable Scope in Super Projects
  • Part IV: CMake ExternalProject_Add or Git Submodules
  • Part V: ????

Please let me know if you see any errors or if I have helped you use CMake. Those things will keep me motivated.

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:

cd best_of_the_cyruses/

Now you’ll do:

cd best_of_the_cyruses/
mkdir build/
cd build/
cmake ..

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 billy_ray project:

# Not required, but nice to put.
cmake_minimum_required ( VERSION 2.8 FATAL_ERROR )
project ( best_of_billy_ray )

# Required
add_executable ( billy_ray
	somebody_new.cpp )

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

This is part of my ongoing series: The CMake you don’t know, but will love.

The RMarkdown documentation is pretty good, but I was having a problem with outputting my pictures to SVG instead of PNG. First, I tried putting the following into R chunk option:

```{r limited_x_reference_lengths, fig.width=4, fig.height=3, fig.ext='svg', message=F}

Unfortunately, that changed the extension of the file without changing the picture format type. After googling for a bit, I noticed in a stackoverflow question that there was another option: dev. So I ended up with the following code:

```{r limited_x_reference_lengths, fig.width=4, fig.height=3, dev='svg', message=F}

My old defunct site

Originally, I was running Wordpress on our family NAS, but I found it to be more of a headache than fun for a few reason.

Where are all the good themes?

This bothered me quite a bit. Nearly all the themes were paid for. And for me, I didn’t like that because I like open source and free software. Plus, many of the great looking themes were aimed at either startups, or companies that sell something.

Why is this so complicated….

In addition to the themes being paid for, trying to read through all of the css, javascript, html, etc. that each theme used was also painful. I don’t mind complexity and actually quite enjoy it :) but, for my blog and website, I really needed something simple.

Security, security, security.

Eventually we are going to build a server for family things, but I’m going to make sure I have full control over what’s going on, who can access what, and so forth. My Synology NAS is awesome, but I’ve always felt a bit detached from the operating system and the packages installed there. Since I’ve started to become a linux power user, I decided that instead of worrying whether Synology and the packages on the site are secure or not, that it was time to just put my non-sensitive stuff in the cloud.

Jekyll and Github pages, yay!

To the rescue was Jekyll and Github pages. I feel like Jekyll is a bit complicated to use, but not too bad. And now that I understand it better, I love that everything is just static content. And Github <3, awesome as usual, allows you to have your own static website on their servers. Plus! If you have your own domain name, they can handle that as well.

So, this isn’t a very polished blog post, but I just had to put at least one post up before I got really busy with work again.

I’ll post results as I come by them :)