Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@
*.swp
*.swo
*.DS_Store

# Local build directories for examples
02-cmake/examples/**/build*/
.idea
21 changes: 19 additions & 2 deletions 02-cmake/02-cmake.tex
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,25 @@ \section{CMake}
\end{lstlisting}
\end{frame}

\begin{frame}{CMake demo}
Demo
\begin{frame}[fragile]{Examples and demo}
Example projects are included with this lecture under:
\begin{itemize}
\item \texttt{02-cmake/examples/steps} — step-by-step progression
\item \texttt{02-cmake/examples/hello} — minimal project
\item \texttt{02-cmake/examples/modern} — target-based project with tests and presets
\end{itemize}
Quick start (Unix, Ninja):
\begin{lstlisting}[language=bash]
cd 02-cmake/examples/steps/02-hello-cmake
cmake -S . -B build -G Ninja -D CMAKE_BUILD_TYPE=Debug
cmake --build build
\end{lstlisting}
Quick start (Windows, Visual Studio):
\begin{lstlisting}[language=bash]
cd 02-cmake\examples\steps\02-hello-cmake
cmake -S . -B build -G "Visual Studio 17 2022" -A x64
cmake --build build --config Debug
\end{lstlisting}
\end{frame}

\begin{frame}
Expand Down
10 changes: 10 additions & 0 deletions 02-cmake/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# CMake Examples

This folder contains cross‑platform examples aligned with the CMake lecture.

- `steps/` — incremental, step‑by‑step walkthrough (recommended start).
- `hello/` — minimal single‑target example.
- `modern/` — target‑based layout with library, tests, presets, and packaging.

See each subfolder’s README for OS/generator specific commands.

6 changes: 6 additions & 0 deletions 02-cmake/examples/hello/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.16)
project(HelloCMake LANGUAGES CXX)

add_executable(hello src/main.cpp)
target_compile_features(hello PRIVATE cxx_std_20)

22 changes: 22 additions & 0 deletions 02-cmake/examples/hello/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": 3,
"cmakeMinimumRequired": { "major": 3, "minor": 22 },
"configurePresets": [
{ "name": "ninja-debug", "generator": "Ninja", "binaryDir": "build/ninja-debug", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } },
{ "name": "ninja-release", "generator": "Ninja", "binaryDir": "build/ninja-release", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } },
{ "name": "unix-make-debug", "generator": "Unix Makefiles", "binaryDir": "build/unix-debug", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } },
{ "name": "xcode-debug", "generator": "Xcode", "binaryDir": "build/xcode-debug" },
{ "name": "vs2022-x64-debug", "generator": "Visual Studio 17 2022", "binaryDir": "build/vs2022/x64", "architecture": { "value": "x64", "strategy": "external" } },
{ "name": "mingw-release", "generator": "MinGW Makefiles", "binaryDir": "build/mingw-release", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } }
],
"buildPresets": [
{ "name": "build-ninja-debug", "configurePreset": "ninja-debug" },
{ "name": "build-ninja-release", "configurePreset": "ninja-release" },
{ "name": "build-unix-debug", "configurePreset": "unix-make-debug" },
{ "name": "build-xcode-debug", "configurePreset": "xcode-debug" },
{ "name": "build-vs-debug", "configurePreset": "vs2022-x64-debug", "configuration": "Debug" },
{ "name": "build-vs-release", "configurePreset": "vs2022-x64-debug", "configuration": "Release" },
{ "name": "build-mingw-release", "configurePreset": "mingw-release" }
]
}

28 changes: 28 additions & 0 deletions 02-cmake/examples/hello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Hello Example (Cross‑platform)

Build with your preferred generator:

- Generic (works everywhere):
- `cmake -S . -B build`
- `cmake --build build`
- Run: `./build/hello` (Linux/macOS) or `build\hello.exe` (Windows)

- Ninja:
- `cmake -S . -B build-ninja -G Ninja -D CMAKE_BUILD_TYPE=Release`
- `cmake --build build-ninja`

- Unix Makefiles (Linux/macOS):
- `cmake -S . -B build-make -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug`
- `cmake --build build-make`

- Visual Studio (Windows, x64):
- `cmake -S . -B build-vs -G "Visual Studio 17 2022" -A x64`
- `cmake --build build-vs --config Release`
- Or open `build-vs/HelloCMake.sln` in Visual Studio.

- MinGW (Windows):
- `cmake -S . -B build-mingw -G "MinGW Makefiles" -D CMAKE_BUILD_TYPE=Release`
- `cmake --build build-mingw`

Presets are provided in `CMakePresets.json` for common setups.

7 changes: 7 additions & 0 deletions 02-cmake/examples/hello/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>

int main() {
std::cout << "Hello, CMake!" << std::endl;
return 0;
}

34 changes: 34 additions & 0 deletions 02-cmake/examples/modern/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.16)
project(ModernCMakeExamples LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

option(BUILD_TESTS "Build unit tests" ON)

add_subdirectory(lib)
add_subdirectory(src)

if(BUILD_TESTS)
enable_testing()
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
)
FetchContent_MakeAvailable(googletest)
add_subdirectory(tests)
endif()

install(TARGETS app add EXPORT modernTargets
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
INCLUDES DESTINATION include
)
install(DIRECTORY lib/include/ DESTINATION include)
install(EXPORT modernTargets NAMESPACE modern:: DESTINATION lib/cmake/modern)

include(CPack)

27 changes: 27 additions & 0 deletions 02-cmake/examples/modern/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": 3,
"cmakeMinimumRequired": { "major": 3, "minor": 22 },
"configurePresets": [
{ "name": "ninja-debug", "generator": "Ninja", "binaryDir": "build/ninja-debug", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } },
{ "name": "ninja-release", "generator": "Ninja", "binaryDir": "build/ninja-release", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } },
{ "name": "unix-make-debug","generator": "Unix Makefiles", "binaryDir": "build/unix-debug", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } },
{ "name": "xcode-debug", "generator": "Xcode", "binaryDir": "build/xcode-debug" },
{ "name": "vs2022-x64", "generator": "Visual Studio 17 2022", "binaryDir": "build/vs2022/x64",
"architecture": { "value": "x64", "strategy": "external" } },
{ "name": "mingw-release", "generator": "MinGW Makefiles", "binaryDir": "build/mingw-release", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } }
],
"buildPresets": [
{ "name": "build-ninja-debug", "configurePreset": "ninja-debug" },
{ "name": "build-ninja-release", "configurePreset": "ninja-release" },
{ "name": "build-unix-debug", "configurePreset": "unix-make-debug" },
{ "name": "build-xcode-debug", "configurePreset": "xcode-debug" },
{ "name": "build-vs-debug", "configurePreset": "vs2022-x64", "configuration": "Debug" },
{ "name": "build-vs-release", "configurePreset": "vs2022-x64", "configuration": "Release" },
{ "name": "build-mingw-release", "configurePreset": "mingw-release" }
],
"testPresets": [
{ "name": "test-ninja-debug", "configurePreset": "ninja-debug" },
{ "name": "test-vs-debug", "configurePreset": "vs2022-x64", "configuration": "Debug" }
]
}

46 changes: 46 additions & 0 deletions 02-cmake/examples/modern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Modern CMake Example (Cross‑platform)

Build options:

- Ninja (fast, single‑config):
- `cmake --preset ninja-debug`
- `cmake --build --preset build-ninja-debug`
- Run: `./build/ninja-debug/app` or `build\ninja-debug\app.exe`

- Unix Makefiles (Linux/macOS):
- `cmake --preset unix-make-debug`
- `cmake --build --preset build-unix-debug`

- Visual Studio 2022 (Windows, x64):
- `cmake --preset vs2022-x64`
- `cmake --build --preset build-vs-debug`
- Or open `build/vs2022/x64/ModernCMakeExamples.sln` and build.

- MinGW (Windows):
- `cmake --preset mingw-release`
- `cmake --build --preset build-mingw-release`

- Xcode (macOS):
- `cmake --preset xcode-debug`
- `cmake --build --preset build-xcode-debug`

Tests:

- After configuring with any preset that enables tests (all of the above), run:
- `ctest --preset test-ninja-debug` (Ninja)
- `ctest --preset test-vs-debug` (Visual Studio)
- Or from a build dir: `ctest --output-on-failure -j`

Cross‑compiling (ARM, example):

```
cmake -S . -B build-arm \
-DCMAKE_TOOLCHAIN_FILE=toolchains/arm-none-eabi.cmake \
-D CMAKE_BUILD_TYPE=Release
cmake --build build-arm
```

Packaging:

- `cpack -G ZIP` from the configured build directory to create an archive.

4 changes: 4 additions & 0 deletions 02-cmake/examples/modern/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_library(add src/add.cpp)
target_include_directories(add PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_features(add PUBLIC cxx_std_20)

8 changes: 8 additions & 0 deletions 02-cmake/examples/modern/lib/include/add/add.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

namespace addlib {

int add(int x, int y);

} // namespace addlib

8 changes: 8 additions & 0 deletions 02-cmake/examples/modern/lib/src/add.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "add/add.h"

namespace addlib {

int add(int x, int y) { return x + y; }

} // namespace addlib

6 changes: 6 additions & 0 deletions 02-cmake/examples/modern/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_executable(app src/main.cpp)
target_link_libraries(app PRIVATE add)
target_compile_features(app PRIVATE cxx_std_20)

set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT app)

10 changes: 10 additions & 0 deletions 02-cmake/examples/modern/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
#include "add/add.h"

int main() {
const int a = 5;
const int b = 3;
std::cout << a << " + " << b << " = " << addlib::add(a, b) << std::endl;
return 0;
}

6 changes: 6 additions & 0 deletions 02-cmake/examples/modern/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_executable(app_tests tests.cpp)
target_link_libraries(app_tests PRIVATE add gtest_main)

include(GoogleTest)
gtest_discover_tests(app_tests)

12 changes: 12 additions & 0 deletions 02-cmake/examples/modern/tests/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <gtest/gtest.h>
#include "add/add.h"

TEST(AddLib, SumsPositive) {
EXPECT_EQ(addlib::add(2, 2), 4);
EXPECT_EQ(addlib::add(5, 3), 8);
}

TEST(AddLib, SumsNegative) {
EXPECT_EQ(addlib::add(-2, -3), -5);
}

6 changes: 6 additions & 0 deletions 02-cmake/examples/modern/toolchains/arm-none-eabi.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)

23 changes: 23 additions & 0 deletions 02-cmake/examples/steps/01-hello-plain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 01 — Hello (no CMake)

Linux/macOS (g++):

```
g++ -std=c++20 -O2 -o hello main.cpp
./hello
```

Windows (Visual Studio Developer Command Prompt):

```
cl /EHsc /std:c++20 main.cpp
main.exe
```

Windows (MinGW):

```
g++ -std=c++20 -O2 -o hello.exe main.cpp
hello.exe
```

7 changes: 7 additions & 0 deletions 02-cmake/examples/steps/01-hello-plain/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>

int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}

5 changes: 5 additions & 0 deletions 02-cmake/examples/steps/02-hello-cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.16)
project(HelloCMake LANGUAGES CXX)
add_executable(hello src/main.cpp)
target_compile_features(hello PRIVATE cxx_std_20)

32 changes: 32 additions & 0 deletions 02-cmake/examples/steps/02-hello-cmake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 02 — Hello (CMake minimal)

Generic:

```
cmake -S . -B build
cmake --build build
./build/hello # Linux/macOS
build\\hello.exe # Windows
```

Ninja:

```
cmake -S . -B build-ninja -G Ninja -D CMAKE_BUILD_TYPE=Release
cmake --build build-ninja
```

Visual Studio (Windows x64):

```
cmake -S . -B build-vs -G "Visual Studio 17 2022" -A x64
cmake --build build-vs --config Debug
```

Unix Makefiles (Linux/macOS):

```
cmake -S . -B build-make -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug
cmake --build build-make
```

7 changes: 7 additions & 0 deletions 02-cmake/examples/steps/02-hello-cmake/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>

int main() {
std::cout << "Hello, CMake!" << std::endl;
return 0;
}

10 changes: 10 additions & 0 deletions 02-cmake/examples/steps/03-lib-cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.16)
project(LibExample LANGUAGES CXX)

add_library(add lib/src/add.cpp)
target_include_directories(add PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib/include)

add_executable(app src/main.cpp)
target_link_libraries(app PRIVATE add)
target_compile_features(app PRIVATE cxx_std_20)

15 changes: 15 additions & 0 deletions 02-cmake/examples/steps/03-lib-cmake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 03 — Library + app

Configure and build (generic):

```
cmake -S . -B build
cmake --build build
./build/app # Linux/macOS
build\\app.exe # Windows
```

Notes:
- `add` is a simple library linked to `app`.
- Headers are exposed via `target_include_directories(add PUBLIC include)`.

Loading