cmake
cross-platform build system generator
TLDR
Generate build files
SYNOPSIS
cmake [options] source-dir
DESCRIPTION
cmake is a cross-platform build system generator. It reads platform-independent CMakeLists.txt project descriptions and generates native build files for the user's chosen build tool, including Unix Makefiles, Ninja, Visual Studio solutions, and Xcode projects.
The typical workflow involves a configure step that generates build files in an out-of-source build directory, followed by a build step that invokes the native build tool. CMake handles dependency detection, compiler feature checks, and library discovery through its find-module system.
CMake is the dominant build system for C and C++ projects, and also supports Fortran, CUDA, and other languages. It includes CTest for test automation and CPack for creating distributable packages.
PARAMETERS
-B dir
Build directory-S dir
Source directory (default: current)-G generator
Build system generator (Unix Makefiles, Ninja, Xcode)-D var=value
Set CMake variable--build dir
Build project--install dir
Install project--preset name
Use configure preset-j N
Parallel build jobs
BUILD TYPES
-DCMAKE_BUILD_TYPE=type
- Debug - No optimization, debug symbols
- Release - Optimized, no debug symbols
- RelWithDebInfo - Optimized with debug symbols
- MinSizeRel - Optimized for size
WORKFLOW
cmake -B build -DCMAKE_BUILD_TYPE=Release
# Build
cmake --build build
# Parallel build
cmake --build build -j 8
# Install
sudo cmake --install build
# Run tests
cd build && ctest
# Clean build
cmake --build build --target clean
CMAKELISTS.TXT
Simple example:
project(MyProject)
add_executable(myapp main.cpp utils.cpp)
target_link_libraries(myapp pthread)
COMMON OPTIONS
cmake -B build -DCMAKE_CXX_COMPILER=clang++
# Set install prefix
cmake -B build -DCMAKE_INSTALL_PREFIX=/usr/local
# Use Ninja generator
cmake -B build -G Ninja
# Verbose build
cmake --build build --verbose
FEATURES
- Cross-platform builds
- Out-of-source builds
- Generator expressions
- Find modules for libraries
- Testing support (CTest)
- Packaging (CPack)
- IDE integration
CAVEATS
Complex syntax for advanced features. Learning curve steep. Generated files can be large. Cache issues require cleaning. Version differences cause compatibility issues. Some find it overcomplicated for simple projects.
HISTORY
CMake was created by Bill Hoffman and Ken Martin at Kitware in 2000 to address the complexity of cross-platform builds, becoming widely adopted by 2010.
