Cmake quickstart

Android ndk uses cmake, Qt6 use cmake by default. It's such an important tool in C++ world now. Let's have a look how to use it.

Quick start

Cmake's configuration is in CMakeLists.txt file

cmake_minimum_required(VERSION 3.5)

project(play-cc LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(play-cc src/main.cpp)

Generate a build in folder build with: cmake -B build .

Build with: cmake --build build

Open IDE with cmake --open build

find_package(SDL2 REQUIRED)
target_include_directories(play-cc PRIVATE ${SDL2_INCLUDE_DIR})
target_link_libraries(play-cc PRIVATE ${SDL2_LIBRARY})

Use vcpkg

Use cmake with vcpkg need to include the toolchain file located under your vcpkg installation.

include(C:/Tools/vcpkg/scripts/buildsystems/vcpkg.cmake)

or add command line argument -DCMAKE_TOOLCHAIN_FILE=C:/Tools/vcpkg/scripts/buildsystems/vcpkg.cmake

DEFINE for cmake

target_compile_definitions(main PRIVATE UNICODE FOO=1 BAR=1)

Set arch

Generate a build with cmake -B build . -T host=x64 -A x64

Set debug/release mode

cmake -DCMAKE_BUILD_TYPE=Debug

Set build mode

cmake –-build build --config Release

cmake --build build --config Debug

More tutorials are available here

https://cmake.org/cmake/help/book/mastering-cmake/index.html