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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@
*.swp
*.swo
*.DS_Store
.idea
160 changes: 160 additions & 0 deletions 02-cmake/02-cmake.tex
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ \section{Build systems history}
\item Ninja
\item Meson
\item Bazel
\item Visual Studio (MSBuild)
\end{itemize}
\end{frame}

Expand Down Expand Up @@ -264,6 +265,15 @@ \section{Build systems history}
\end{itemize}
\end{frame}

\begin{frame}{Visual Studio/MSBuild}
\begin{itemize}
\item Microsoft native build system and toolchain on Windows.
\item Uses MSBuild with \texttt{.sln}/\texttt{.vcxproj} project files.
\item Deep IDE integration; also builds from CLI via \texttt{msbuild} or \texttt{cmake --build}.
\item Often generated by CMake for Windows targets.
\end{itemize}
\end{frame}

\section{CMake}

\begin{frame}{Why CMake?}
Expand All @@ -276,6 +286,25 @@ \section{CMake}
\end{itemize}
\end{frame}

\begin{frame}[fragile]{CMake with Visual Studio generator}
Generate a Visual Studio solution and build from the command line.
\begin{itemize}
\item Configure (choose VS version and architecture):
\begin{lstlisting}[language=bash]
cmake -S . -B build -G "Visual Studio 18 2026" -A x64
\end{lstlisting}
\item Build (Debug/Release via --config):
\begin{lstlisting}[language=bash]
cmake --build build --config Release
\end{lstlisting}
\item Run the executable:
\begin{lstlisting}[language=bash]
build\\Release\\main.exe
\end{lstlisting}
\item Alternatively open \texttt{build/HelloAdd.sln} in Visual Studio and build F7.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Download CMake}
\begin{itemize}
\item Official website: \url{https://cmake.org}
Expand Down Expand Up @@ -396,6 +425,137 @@ \section{CMake}
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Modern target-based CMake}
Prefer target properties over global variables.
\begin{block}{CMakeLists.txt}
\begin{lstlisting}
cmake_minimum_required(VERSION 3.16)
project(App)

add_library(lib STATIC src/lib.cpp)
target_include_directories(lib PUBLIC include)

add_executable(app src/main.cpp)
target_link_libraries(app PRIVATE lib)
target_compile_features(app PRIVATE cxx_std_20)
\end{lstlisting}
\end{block}
PRIVATE/PUBLIC/INTERFACE control propagation of usage requirements.
\end{frame}

\begin{frame}[fragile]{Project layout and add\_subdirectory}
Example tree and minimal setup.
\begin{lstlisting}
project/
CMakeLists.txt
src/ CMakeLists.txt main.cpp
lib/ CMakeLists.txt include/lib/lib.h lib.cpp
\end{lstlisting}
\begin{block}{Top-level CMakeLists.txt}
\begin{lstlisting}
cmake_minimum_required(VERSION 3.16)
project(App)
add_subdirectory(lib)
add_subdirectory(src)
\end{lstlisting}
\end{block}
\end{frame}

\begin{frame}[fragile]{Dependencies: find\_package vs FetchContent}
Use packages when available; fetch when needed.
\begin{block}{find\_package (fmt)}
\begin{lstlisting}
find_package(fmt CONFIG REQUIRED)
target_link_libraries(app PRIVATE fmt::fmt)
\end{lstlisting}
\end{block}
\begin{block}{FetchContent (GoogleTest)}
\begin{lstlisting}
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip)
FetchContent_MakeAvailable(googletest)
\end{lstlisting}
\end{block}
\end{frame}

\begin{frame}[fragile]{Testing with CTest and GoogleTest}
Integrate tests and run via CTest.
\begin{block}{CMakeLists.txt}
\begin{lstlisting}
enable_testing()
add_executable(app_tests tests/tests.cpp)
target_link_libraries(app_tests PRIVATE gtest_main)
include(GoogleTest)
gtest_discover_tests(app_tests)
\end{lstlisting}
\end{block}
Run tests:
\begin{lstlisting}[language=bash]
ctest --output-on-failure -j
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{CMake Presets (CMakePresets.json)}
Shareable configure/build/test profiles for CLI and IDEs.
\begin{block}{CMakePresets.json}
\begin{lstlisting}
{
"version": 3,
"cmakeMinimumRequired": {"major": 3, "minor": 22},
"configurePresets": [
{"name":"ninja-debug","generator":"Ninja",
"binaryDir":"build/debug",
"cacheVariables":{"CMAKE_BUILD_TYPE":"Debug"}}
],
"buildPresets": [
{"name":"build-debug","configurePreset":"ninja-debug"}
],
"testPresets": [
{"name":"test-debug","configurePreset":"ninja-debug"}
]
}
\end{lstlisting}
\end{block}
\begin{lstlisting}[language=bash]
cmake --preset ninja-debug
cmake --build --preset build-debug
ctest --preset test-debug
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Cross-compiling and toolchains}
Point CMake to a toolchain file for non-host targets.
\begin{lstlisting}[language=bash]
cmake -S . -B build-arm \
-DCMAKE_TOOLCHAIN_FILE=toolchains/arm-none-eabi.cmake
\end{lstlisting}
\begin{block}{toolchains/arm-none-eabi.cmake}
\begin{lstlisting}
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++)
\end{lstlisting}
\end{block}
\end{frame}

\begin{frame}[fragile]{Install, export, and CPack}
Install targets and generate simple packages.
\begin{block}{CMakeLists.txt}
\begin{lstlisting}
install(TARGETS app EXPORT appTargets RUNTIME DESTINATION bin)
install(EXPORT appTargets NAMESPACE app:: DESTINATION lib/cmake/app)
include(CPack)
\end{lstlisting}
\end{block}
Create an archive package:
\begin{lstlisting}[language=bash]
cpack -G ZIP
\end{lstlisting}
\end{frame}

\begin{frame}{CMake demo}
Demo
\end{frame}
Expand Down
2 changes: 1 addition & 1 deletion 02-cmake/02-cmake.toc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
\beamer@sectionintoc {1}{Building C++ projects}{3}{0}{1}
\beamer@sectionintoc {2}{Build systems history}{9}{0}{2}
\beamer@sectionintoc {3}{CMake}{16}{0}{3}
\beamer@sectionintoc {3}{CMake}{17}{0}{3}