Building Software With CMake

Introduction
During software development, you need a build system to compile your software over and over again. For simple program, a simple  is usually sufficient.

However, when the time has come to release your software to others, you often need something more elaborate to help users compile your application from source, check that all prerequisites are available on their system, and also to install your compiled program and all the files that come with it.

In the GNU/Linux world, many software packages are built using the autotools. These tools have made it possible for GNU project components to be cross-platform and support multiple UNIX flavors and multiple CPU architectures.

However, though this build system is still very useful today, it is also far from being perfect. It has a steep learning curve for new developers, which basically have to get familiar with a complex set of tools and macro language. Debugging user issues can also be very difficult, because of the complexity of the generated Makefiles and scripts put in the end of the users. See this article for criticism about the  and.

Fortunately, more modern, smarter, and easier to learn alternatives now exist. Let's have a look at CMake.

Building a simple program with CMake
First, install CMake. For example, if you are using a Debian based distro: sudo apt-get install cmake

Let's take an example program, which doesn't have a build system yet: devmem2.c

To build it with, you just need to create a   file, such as:

cmake_minimum_required(VERSION 2.8) project(devmem2) add_executable(devmem2 devmem2.c) install(TARGETS devmem2 DESTINATION "/usr/sbin")

won't actually build the software by itself. It will first check for available compiling software and depending on your operating system, generate a native build environment (Makefile on Unix, project / workspace on Windows...). Here's the command to do it:

cmake.

Now, you can build and install your software:

make sudo make install

Slightly more elaborate examples
What's good about, compared to the   and  , is that you just have to tell it which source   and   files you have, and which libraries you will compile your program with, and it will automatically figure out how to build it. That's no rocket science anyway, but with the, you have to explicit many more things.

Here is another, easy to understand example of a program depending on an internal library and on  and  :

PROJECT(MyProject C) ADD_LIBRARY(MyLibrary STATIC libSource.c) ADD_EXECUTABLE(MyProgram main.c) TARGET_LINK_LIBRARIES(MyProgram MyLibrary z m)

You will find many nice introductory information about  on the Internet, like this Linux Journal article.

Debian packaging
This can be useful for people creating software for Ubuntu on OMAP, but the following applies to any other use in a Debian system, of course.

In the same way it supports the, the Debian build system also has native support for.

Back to our  example above, here's what you need to do to create a Debian package for your application.

You will need a few packages to build your Debian package: sudo apt-get install dh-make devscripts cdbs

First, put your source code in a  directory, and from this directory, create a basing Debian skeleton, for example: dh_make --createorig -c gpl2 -s -e michael@free-electrons.com

Explore the  directory, and adapt the files according to your application. You probably will have to remove multiple  example files that are not needed in your package.

You will see that a very small number of files are needed to build a Debian package with no Lintian warnings! devmem2-1.0.0/ devmem2-1.0.0/debian/ devmem2-1.0.0/debian/devmem2.manpages devmem2-1.0.0/debian/compat devmem2-1.0.0/debian/changelog devmem2-1.0.0/debian/rules devmem2-1.0.0/debian/control devmem2-1.0.0/debian/copyright devmem2-1.0.0/debian/source/ devmem2-1.0.0/debian/source/format devmem2-1.0.0/devmem2.1 devmem2-1.0.0/CMakeLists.txt devmem2-1.0.0/devmem2.c

The complete sources can be found on http://free-electrons.com/pub/ubuntu/cmake/devmem2-1.0.0.tar.bz2. Don't hesitate to use them as a starting template, instead of starting from scratch with !

At least, let's see how simple the  file is: include /usr/share/cdbs/1/rules/debhelper.mk include /usr/share/cdbs/1/class/cmake.mk
 * 1) !/usr/bin/make -f

To clear all Lintian errors and warnings, you will need to file a Debian Intent to Package. See the Debian Mentors FAQ if you wish to contribute a package to Debian and maintain it.