Site Tools


wiki:cmake-cmakelists-txt

Table of Contents

CMake CMakeLists.txt

CMakeLists.txt is the build configuration file. It describes what to build, where sources are, dependencies, and build options. Written in CMake's scripting language (imperative, case-insensitive commands).

Minimal example:

cmake_minimum_required(VERSION 3.20)
project(MyApp)
 
add_executable(myapp main.cpp)

This declares a project named MyApp (creates a binary myapp from main.cpp). Run cmake . to generate build files in the current directory (or cmake -B build for out-of-source).

Common commands:

cmake_minimum_required(VERSION X.Y)  # minimum CMake version
project(name LANGUAGES C CXX)        # project name, languages used
add_executable(name source.cpp)      # executable target
add_library(name source.cpp)         # library target
target_link_libraries(exe lib)       # link target to library
find_package(PackageName)            # find external package
include_directories(/path/to/headers) # add include path
set(VARIABLE value)                  # set variable

Commands are functions: command(arg1 arg2 ...). Lists use semicolons or spaces. Variables are ${VAR}. Comments start with #.

Structure:

  1. cmake_minimum_required (must be first)
  2. project() declaration
  3. find_package() calls for dependencies
  4. add_executable() or add_library() for targets
  5. target_link_libraries() and other target configuration
  6. install() rules (optional)

CMake scans source files for dependencies. It's declarative about what you want to build; the generator handles how to actually compile it for your platform.

wiki/cmake-cmakelists-txt.md · Last modified: (external edit)