Skip to content

Commit 840f67d

Browse files
authored
Add CMake examples: step-by-step tutorials, modern features, and presets. (#33)
1 parent d5f1a63 commit 840f67d

File tree

61 files changed

+741
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+741
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@
4646
*.swp
4747
*.swo
4848
*.DS_Store
49+
50+
# Local build directories for examples
51+
02-cmake/examples/**/build*/
4952
.idea

02-cmake/02-cmake.tex

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,25 @@ \section{CMake}
556556
\end{lstlisting}
557557
\end{frame}
558558

559-
\begin{frame}{CMake demo}
560-
Demo
559+
\begin{frame}[fragile]{Examples and demo}
560+
Example projects are included with this lecture under:
561+
\begin{itemize}
562+
\item \texttt{02-cmake/examples/steps} — step-by-step progression
563+
\item \texttt{02-cmake/examples/hello} — minimal project
564+
\item \texttt{02-cmake/examples/modern} — target-based project with tests and presets
565+
\end{itemize}
566+
Quick start (Unix, Ninja):
567+
\begin{lstlisting}[language=bash]
568+
cd 02-cmake/examples/steps/02-hello-cmake
569+
cmake -S . -B build -G Ninja -D CMAKE_BUILD_TYPE=Debug
570+
cmake --build build
571+
\end{lstlisting}
572+
Quick start (Windows, Visual Studio):
573+
\begin{lstlisting}[language=bash]
574+
cd 02-cmake\examples\steps\02-hello-cmake
575+
cmake -S . -B build -G "Visual Studio 17 2022" -A x64
576+
cmake --build build --config Debug
577+
\end{lstlisting}
561578
\end{frame}
562579

563580
\begin{frame}

02-cmake/examples/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# CMake Examples
2+
3+
This folder contains cross‑platform examples aligned with the CMake lecture.
4+
5+
- `steps/` — incremental, step‑by‑step walkthrough (recommended start).
6+
- `hello/` — minimal single‑target example.
7+
- `modern/` — target‑based layout with library, tests, presets, and packaging.
8+
9+
See each subfolder’s README for OS/generator specific commands.
10+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(HelloCMake LANGUAGES CXX)
3+
4+
add_executable(hello src/main.cpp)
5+
target_compile_features(hello PRIVATE cxx_std_20)
6+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"version": 3,
3+
"cmakeMinimumRequired": { "major": 3, "minor": 22 },
4+
"configurePresets": [
5+
{ "name": "ninja-debug", "generator": "Ninja", "binaryDir": "build/ninja-debug", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } },
6+
{ "name": "ninja-release", "generator": "Ninja", "binaryDir": "build/ninja-release", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } },
7+
{ "name": "unix-make-debug", "generator": "Unix Makefiles", "binaryDir": "build/unix-debug", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } },
8+
{ "name": "xcode-debug", "generator": "Xcode", "binaryDir": "build/xcode-debug" },
9+
{ "name": "vs2022-x64-debug", "generator": "Visual Studio 17 2022", "binaryDir": "build/vs2022/x64", "architecture": { "value": "x64", "strategy": "external" } },
10+
{ "name": "mingw-release", "generator": "MinGW Makefiles", "binaryDir": "build/mingw-release", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } }
11+
],
12+
"buildPresets": [
13+
{ "name": "build-ninja-debug", "configurePreset": "ninja-debug" },
14+
{ "name": "build-ninja-release", "configurePreset": "ninja-release" },
15+
{ "name": "build-unix-debug", "configurePreset": "unix-make-debug" },
16+
{ "name": "build-xcode-debug", "configurePreset": "xcode-debug" },
17+
{ "name": "build-vs-debug", "configurePreset": "vs2022-x64-debug", "configuration": "Debug" },
18+
{ "name": "build-vs-release", "configurePreset": "vs2022-x64-debug", "configuration": "Release" },
19+
{ "name": "build-mingw-release", "configurePreset": "mingw-release" }
20+
]
21+
}
22+

02-cmake/examples/hello/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Hello Example (Cross‑platform)
2+
3+
Build with your preferred generator:
4+
5+
- Generic (works everywhere):
6+
- `cmake -S . -B build`
7+
- `cmake --build build`
8+
- Run: `./build/hello` (Linux/macOS) or `build\hello.exe` (Windows)
9+
10+
- Ninja:
11+
- `cmake -S . -B build-ninja -G Ninja -D CMAKE_BUILD_TYPE=Release`
12+
- `cmake --build build-ninja`
13+
14+
- Unix Makefiles (Linux/macOS):
15+
- `cmake -S . -B build-make -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug`
16+
- `cmake --build build-make`
17+
18+
- Visual Studio (Windows, x64):
19+
- `cmake -S . -B build-vs -G "Visual Studio 17 2022" -A x64`
20+
- `cmake --build build-vs --config Release`
21+
- Or open `build-vs/HelloCMake.sln` in Visual Studio.
22+
23+
- MinGW (Windows):
24+
- `cmake -S . -B build-mingw -G "MinGW Makefiles" -D CMAKE_BUILD_TYPE=Release`
25+
- `cmake --build build-mingw`
26+
27+
Presets are provided in `CMakePresets.json` for common setups.
28+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <iostream>
2+
3+
int main() {
4+
std::cout << "Hello, CMake!" << std::endl;
5+
return 0;
6+
}
7+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(ModernCMakeExamples LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_CXX_EXTENSIONS OFF)
7+
8+
option(BUILD_TESTS "Build unit tests" ON)
9+
10+
add_subdirectory(lib)
11+
add_subdirectory(src)
12+
13+
if(BUILD_TESTS)
14+
enable_testing()
15+
include(FetchContent)
16+
FetchContent_Declare(
17+
googletest
18+
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
19+
)
20+
FetchContent_MakeAvailable(googletest)
21+
add_subdirectory(tests)
22+
endif()
23+
24+
install(TARGETS app add EXPORT modernTargets
25+
RUNTIME DESTINATION bin
26+
LIBRARY DESTINATION lib
27+
ARCHIVE DESTINATION lib
28+
INCLUDES DESTINATION include
29+
)
30+
install(DIRECTORY lib/include/ DESTINATION include)
31+
install(EXPORT modernTargets NAMESPACE modern:: DESTINATION lib/cmake/modern)
32+
33+
include(CPack)
34+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"version": 3,
3+
"cmakeMinimumRequired": { "major": 3, "minor": 22 },
4+
"configurePresets": [
5+
{ "name": "ninja-debug", "generator": "Ninja", "binaryDir": "build/ninja-debug", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } },
6+
{ "name": "ninja-release", "generator": "Ninja", "binaryDir": "build/ninja-release", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } },
7+
{ "name": "unix-make-debug","generator": "Unix Makefiles", "binaryDir": "build/unix-debug", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } },
8+
{ "name": "xcode-debug", "generator": "Xcode", "binaryDir": "build/xcode-debug" },
9+
{ "name": "vs2022-x64", "generator": "Visual Studio 17 2022", "binaryDir": "build/vs2022/x64",
10+
"architecture": { "value": "x64", "strategy": "external" } },
11+
{ "name": "mingw-release", "generator": "MinGW Makefiles", "binaryDir": "build/mingw-release", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } }
12+
],
13+
"buildPresets": [
14+
{ "name": "build-ninja-debug", "configurePreset": "ninja-debug" },
15+
{ "name": "build-ninja-release", "configurePreset": "ninja-release" },
16+
{ "name": "build-unix-debug", "configurePreset": "unix-make-debug" },
17+
{ "name": "build-xcode-debug", "configurePreset": "xcode-debug" },
18+
{ "name": "build-vs-debug", "configurePreset": "vs2022-x64", "configuration": "Debug" },
19+
{ "name": "build-vs-release", "configurePreset": "vs2022-x64", "configuration": "Release" },
20+
{ "name": "build-mingw-release", "configurePreset": "mingw-release" }
21+
],
22+
"testPresets": [
23+
{ "name": "test-ninja-debug", "configurePreset": "ninja-debug" },
24+
{ "name": "test-vs-debug", "configurePreset": "vs2022-x64", "configuration": "Debug" }
25+
]
26+
}
27+

02-cmake/examples/modern/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Modern CMake Example (Cross‑platform)
2+
3+
Build options:
4+
5+
- Ninja (fast, single‑config):
6+
- `cmake --preset ninja-debug`
7+
- `cmake --build --preset build-ninja-debug`
8+
- Run: `./build/ninja-debug/app` or `build\ninja-debug\app.exe`
9+
10+
- Unix Makefiles (Linux/macOS):
11+
- `cmake --preset unix-make-debug`
12+
- `cmake --build --preset build-unix-debug`
13+
14+
- Visual Studio 2022 (Windows, x64):
15+
- `cmake --preset vs2022-x64`
16+
- `cmake --build --preset build-vs-debug`
17+
- Or open `build/vs2022/x64/ModernCMakeExamples.sln` and build.
18+
19+
- MinGW (Windows):
20+
- `cmake --preset mingw-release`
21+
- `cmake --build --preset build-mingw-release`
22+
23+
- Xcode (macOS):
24+
- `cmake --preset xcode-debug`
25+
- `cmake --build --preset build-xcode-debug`
26+
27+
Tests:
28+
29+
- After configuring with any preset that enables tests (all of the above), run:
30+
- `ctest --preset test-ninja-debug` (Ninja)
31+
- `ctest --preset test-vs-debug` (Visual Studio)
32+
- Or from a build dir: `ctest --output-on-failure -j`
33+
34+
Cross‑compiling (ARM, example):
35+
36+
```
37+
cmake -S . -B build-arm \
38+
-DCMAKE_TOOLCHAIN_FILE=toolchains/arm-none-eabi.cmake \
39+
-D CMAKE_BUILD_TYPE=Release
40+
cmake --build build-arm
41+
```
42+
43+
Packaging:
44+
45+
- `cpack -G ZIP` from the configured build directory to create an archive.
46+

0 commit comments

Comments
 (0)