Steve Love
@IAmSteveLove@mastodon.social
@stevelove.bsky.social
https://moderncsharp.blogspot.com/
Show some simple examples
Introduce some important concepts
Hopefully:
help you recognise the general structure of a CMake file
help you get started from scratch
cmake_minimum_required(VERSION 3.28)
project(heating_system)
set(CMAKE_CXX_STANDARD 20)
add_executable(perf_triggers bench.cpp)
target_include_directories(perf_triggers PRIVATE ../full)
include(FetchContent)
FetchContent_Declare(
googlebenchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG v1.9.2
)
FetchContent_MakeAvailable(googlebenchmark)
target_link_libraries(perf_triggers PRIVATE benchmark::benchmark_main)
cmake_minimum_required(VERSION 3.28)
project(heating_system)
set(SOMETHING_STANDARD 20)
gobble_dee_gook(nonsense blah.cpp)
marmalade_something_apples(garbage_patch BABOON something)
include(WibbleFish)
SomeNonsense_Bananas(
googlebenchmark
PET_HAMSTER https://github.com/google/benchmark.git
PEA_SOUP v1.9.2
)
WibbleFish_MarmaladeJam(googlebenchmark)
apples_blah_something(blah BLAH something::something)
#include <iostream>
int main()
{
std::cout << "Hello CMake!\n";
}
cmake_minimum_required(VERSION 3.28)
project(hello)
add_executable(world main.cpp)
Generate the build config in |
|
Build the code |
|
Run the code |
|
Ninja
Is?Build system (like make
)
Independent of tool chain
Fast
No rules!
Tell it what to do explicitly
Configuration by non-humans only
Common build configuration
independent of the actual tools
switch on command line (-G <generator>
)
a consistent process
Build process can be version controlled
multi-platform, multi-target
build process is part of the code
CI Server Friendly
build agent environment can differ
Just use make
?
Multi-file projects
Compilation time
Dependency Management
Compile time
Link time
Avoiding unnecessary recompilation
Proliferation of:
platforms
tools
configurations
environments
etc.
make
Different dialects
dependencies and rules
autotools
Precompiled headers
CMake
Cross-platform build tooling for C++ (and others)
De-facto standard format
A "meta-build"
Makefile
maker for [m]any platform[s]
but more than that
CMake is very powerful. With great power comes
#include <iostream>
#include "message.h"
int main()
{
using namespace std;
cout << "Hello " << message << "!\n";
}
#pragma once
#include <string>
const std::string message = "CMake";
cmake_minimum_required(VERSION 3.28)
project(hello_custom)
add_executable(custom_message main.cpp)
Time for a demo?
| ← Non-standard system include |
| Full path specified |
| ← full paths for headers and libraries ← linked names are platform agnostic |
target_link_libraries(bounce-d
debug sfml-graphics-d
debug sfml-window-d
debug sfml-system-d
optimized sfml-graphics
optimized sfml-window
optimized sfml-system
)
| ← hard-coded path can be passed on cmake command-line |
A stand-alone executable
I want to test!
cmake_minimum_required(VERSION 3.28)
project(gtests)
set(CMAKE_CXX_STANDARD 26)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.17.0.zip
)
FetchContent_MakeAvailable(googletest)
add_executable(gtests tests.cpp)
target_link_libraries(gtests gtest_main)
cmake_minimum_required(VERSION 3.28)
project(catchtests)
set(CMAKE_CXX_STANDARD 26)
Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.7.1
)
FetchContent_MakeAvailable(Catch2)
add_executable(catchtests tests.cpp)
target_link_libraries(catchtests PRIVATE Catch2::Catch2WithMain)
- note how slow to build
multiple targets
dependencies between targets
e.g. tests
Flat file
|
|
Top-level | heating_triggers | tests |
---|---|---|
|
|
|
frankly, don’t bother (unless you already know cmake
)
is popular, and free (with restrictions)
rocks (and free for non-commercial use)