Cmake

From wikinotes

CMake is a cross-platform, and compiler agnostic build-system.

Instead of a platform-native buildfile, cmake provides a scripting language to generate a platform-native buildfile, then run it.

For example, cmake might build a Makefile for gcc/clang, or it might make a VisualStudio buildfile, or an XCode buildfile etc.


Usage

  • Build Instructions are written to CMakeLists.txt.
  • The Buildfile is generated
  • The compiler reads the Buildfile, and compiles the project.

Building with CMake

building the build-instructions

# from your directory containing CMakeLists.txt..

mkdir build
cd build
cmake ..             # build native compilation instructions
 
 
# now using your native compiler (in this example gcc or clang)
# use the generated makefile to compile your project.
# (this process will vary widely depending on the platform you are compiling on)
 
make                 # compile project using cmake-generated Makefile
make clean           # delete the temporary build directory, and (re)build the project from scratch
make install         # install files from temporary build directory into the host system
make clean install   # you can combine arguments to perform multiple steps at once

CMakeLists.txt

The following example is for a hypothetical C project:

  • it comprises of 2x files: main.c and utils.c
  • it gets compiled into 1x executable: myproject(.exe)

CMakeLists.txt

# ============
# Package Info
# ============
cmake_minimum_required(VERSION 2.8)
project(
    "myproject"
    DESCRIPTION "My project that does something"
    VERSION 1.0.0
)
 
 
# ==========
# Build Info
# ==========
add_executable( build/myproject main.c utils.c )
 
 
# ============
# Install Info
# ============
install(
    PROGRAMS     build/myproject
    DESTINATION  bin
)

Target Platforms

cmake unix
cmake windows
cmake macos

Syntax

cmake basics
cmake datatypes
cmake conditionals
cmake expressions
cmake operators
cmake loops
cmake macros
cmake filesystem
cmake general utilities
cmake compile targets

Reference