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:
cmake_minimum_required (must be first)project() declarationfind_package() calls for dependenciesadd_executable() or add_library() for targetstarget_link_libraries() and other target configurationinstall() 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.