2013-06-09 00:00:00 -0700

CMake Quickstart


quickstart

A good plan violently executed now is better than a perfect plan executed next week.

George S. Patton

Intro

While being able to read Makefiles is an important skill, using Make directly is probably not the most efficient use of time. Automation tools such as Ant, autotools, scons, and CMake provide another layer of abstraction over the build process. This layer of abstraction provides easier to use/read api’s, dependency checking, as well as cross platform and cross compilation support.

CMake has a slight learning curve to it, and some rather dense documentation. Here’s a quicksart guide for getting up and running with a basic CMakeLists.txt (config) file for a simple C project.

The only real downside to CMake is that it usually needs to be installed, and is probably not shipped by default on a given platform.

TL;DR

here’s a basic c project skeleton with cmake

basic setup on linux platforms

for a trivial project, two lines is all you need

project (HelloWorld)
add_executable(HelloWorld helloworld.c)
next, run the following in the root of your project
$ cmake .
this will generate a makefile build system a simple project. Afterward, running ‘make’ will build your project

linking against an external library

include_directories

specify an additional directory for the compiler to search for include files

include_directories("include")

specify an additional directory for the linker to serach for libraries

link_directories("lib")

add_library

adds a library to the compilation queue of this project

add_library(submoduleA asubmodule.c)
much like add_executable, will generate object files for submoduleA. CMake works out source dependencies and linkage relationships.

target_link_libraries(HelloWorld asubmodule)

linking against both external and iternal libraries is the same, it just needs to be on the linkers search path.

add_subdirectory

generally, a single CMakeLists.txt specifies for a single executable. You can specify a subdirectory with:

add_subdirectory(asubmodule)

this example project shows all you need to setup a basic project

Usefule functions

cmake_minimum_required

specify the minimum version of cmake required

cmake_minimum_required (VERSION 2.6)

set compiler flags

we can set arguments to specific compilers with:

SET(GCC_LINK_FLAGS  "-std=c11 -Wall")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCC_LINK_FLAGS}")
the fist SET declares a new variable “GCC_LINK_FLAGS” and initializes it with our string of flags we want to send to the linker

the second SET modifies the cmake environment variable CMAKE_C_FLAGS, by concatenating it and our custom flags


comments powered by Disqus