Skip to main content

CMake Overview

A comprehensive, user-friendly guide to CMake organized for easy reference and learning.

Documentation Structure

Introduction

Start here if you're new to CMake:

Basics

Core concepts you'll use daily:

Targets

Understanding executables and libraries:

Dependencies

Managing external libraries:

Project Organization

Structuring larger projects:

Advanced Topics

Level up your CMake skills:

Testing

Adding tests to your project:

Quick References

Common Commands

CMake Basics
# Project setup
cmake_minimum_required(VERSION 3.15)
project(MyProject VERSION 1.0)

# Executables and libraries
add_executable(myapp main.cpp)
add_library(mylib STATIC lib.cpp)

# Linking
target_link_libraries(myapp PRIVATE mylib)

# Include directories
target_include_directories(myapp PRIVATE include/)

# Compile features
target_compile_features(myapp PRIVATE cxx_std_17)

# Finding packages
find_package(Threads REQUIRED)

Build Workflow

Terminal
# Configure
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release

# Build
cmake --build build

# Test
ctest --test-dir build

# Install
cmake --install build --prefix /usr/local

Documentation Conventions

Throughout this knowledge base:

Information Boxes

Provide helpful context and explanations

Best Practices

Highlight recommended approaches

Common Pitfalls

Alert you to potential issues

Anti-Patterns

Identify and avoid bad practices

Code Examples

All code examples are complete and ready to use. They follow modern CMake practices (3.15+) and include:

  • Full listings with syntax highlighting
  • Comments explaining key concepts
  • Practical, real-world scenarios
  • Both simple and complex examples

Finding What You Need

By Topic

  • Getting Started: See Introduction section
  • Day-to-Day Usage: See Basics and Targets sections
  • Project Structure: See Project Organization section
  • External Dependencies: See Dependencies section
  • Advanced Features: See Advanced Topics section

By Use Case

  • Simple Application: First Project → Executables
  • Library Development: Libraries → Target Properties
  • Multi-Library Project: Subdirectories → Linking
  • Using External Libs: find_package() → FetchContent

Remember: CMake is a tool for building software. The best way to learn is by doing. Start with simple projects and gradually incorporate more features as you need them.

Happy building! 🎉