cmake
TLDR
Generate build files
SYNOPSIS
cmake [options] source-dir
DESCRIPTION
cmake is a cross-platform build system generator. It generates native build files (Makefiles, Ninja, Visual Studio, Xcode) from platform-independent CMakeLists.txt files, simplifying the build process across different operating systems.
The tool is widely used for C/C++ projects and supports other languages.
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.


