1. Introduction
- It’s a meta-build system generator.
- How it works:
- We write high-level instructions in a CMakeLists.txt file (platform-agnostic).Then CMake generates build system files for the platform we choose:
- On Linux/Unix → generates Makefile (for make) or build.ninja (for ninja).
- On Windows → generates Visual Studio solutions (.sln).
- On macOS → can generate Xcode projects.
2. Setup
- Linux: Install:
sudo apt install cmake
, Verify:cmake --version
3. How to work with CMake
- Create CMakeLists.txt and resources file
- Create and cd to
project-build
folder & Runcmake <dir-contain-CMakeLists.txt>
(cmake ../
) to set up & generate build system. - Run
cmake --build <dir-contain-buildsystem>
(cmake --build ./
) to actually build/compile the project. - Run the target build (e.g.
./targetName
)
3.1.
CMakeLists.txt
cmake_minimum_required(VERSION 3.0) // specifying a minimum CMake version
project(CmakeprojectName) // set the project name
add_executeable(targetName "headerfile.h" "sourcefile1.cpp" "sourcefile2.cpp") // specified source code files