Skip to content

Commit d5f1a63

Browse files
allnesaobolensk
andauthored
Add Visual Studio (MSBuild) content, examples, and modern CMake practices (#32)
* Add Visual Studio (MSBuild) content, examples, and modern CMake practices * Apply suggestion from @aobolensk --------- Co-authored-by: Arseniy Obolenskiy <gooddoog@student.su>
1 parent 77abda0 commit d5f1a63

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@
4646
*.swp
4747
*.swo
4848
*.DS_Store
49+
.idea

02-cmake/02-cmake.tex

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ \section{Build systems history}
206206
\item Ninja
207207
\item Meson
208208
\item Bazel
209+
\item Visual Studio (MSBuild)
209210
\end{itemize}
210211
\end{frame}
211212

@@ -264,6 +265,15 @@ \section{Build systems history}
264265
\end{itemize}
265266
\end{frame}
266267

268+
\begin{frame}{Visual Studio/MSBuild}
269+
\begin{itemize}
270+
\item Microsoft native build system and toolchain on Windows.
271+
\item Uses MSBuild with \texttt{.sln}/\texttt{.vcxproj} project files.
272+
\item Deep IDE integration; also builds from CLI via \texttt{msbuild} or \texttt{cmake --build}.
273+
\item Often generated by CMake for Windows targets.
274+
\end{itemize}
275+
\end{frame}
276+
267277
\section{CMake}
268278

269279
\begin{frame}{Why CMake?}
@@ -276,6 +286,25 @@ \section{CMake}
276286
\end{itemize}
277287
\end{frame}
278288

289+
\begin{frame}[fragile]{CMake with Visual Studio generator}
290+
Generate a Visual Studio solution and build from the command line.
291+
\begin{itemize}
292+
\item Configure (choose VS version and architecture):
293+
\begin{lstlisting}[language=bash]
294+
cmake -S . -B build -G "Visual Studio 18 2026" -A x64
295+
\end{lstlisting}
296+
\item Build (Debug/Release via --config):
297+
\begin{lstlisting}[language=bash]
298+
cmake --build build --config Release
299+
\end{lstlisting}
300+
\item Run the executable:
301+
\begin{lstlisting}[language=bash]
302+
build\\Release\\main.exe
303+
\end{lstlisting}
304+
\item Alternatively open \texttt{build/HelloAdd.sln} in Visual Studio and build F7.
305+
\end{itemize}
306+
\end{frame}
307+
279308
\begin{frame}[fragile]{Download CMake}
280309
\begin{itemize}
281310
\item Official website: \url{https://cmake.org}
@@ -396,6 +425,137 @@ \section{CMake}
396425
\end{itemize}
397426
\end{frame}
398427

428+
\begin{frame}[fragile]{Modern target-based CMake}
429+
Prefer target properties over global variables.
430+
\begin{block}{CMakeLists.txt}
431+
\begin{lstlisting}
432+
cmake_minimum_required(VERSION 3.16)
433+
project(App)
434+
435+
add_library(lib STATIC src/lib.cpp)
436+
target_include_directories(lib PUBLIC include)
437+
438+
add_executable(app src/main.cpp)
439+
target_link_libraries(app PRIVATE lib)
440+
target_compile_features(app PRIVATE cxx_std_20)
441+
\end{lstlisting}
442+
\end{block}
443+
PRIVATE/PUBLIC/INTERFACE control propagation of usage requirements.
444+
\end{frame}
445+
446+
\begin{frame}[fragile]{Project layout and add\_subdirectory}
447+
Example tree and minimal setup.
448+
\begin{lstlisting}
449+
project/
450+
CMakeLists.txt
451+
src/ CMakeLists.txt main.cpp
452+
lib/ CMakeLists.txt include/lib/lib.h lib.cpp
453+
\end{lstlisting}
454+
\begin{block}{Top-level CMakeLists.txt}
455+
\begin{lstlisting}
456+
cmake_minimum_required(VERSION 3.16)
457+
project(App)
458+
add_subdirectory(lib)
459+
add_subdirectory(src)
460+
\end{lstlisting}
461+
\end{block}
462+
\end{frame}
463+
464+
\begin{frame}[fragile]{Dependencies: find\_package vs FetchContent}
465+
Use packages when available; fetch when needed.
466+
\begin{block}{find\_package (fmt)}
467+
\begin{lstlisting}
468+
find_package(fmt CONFIG REQUIRED)
469+
target_link_libraries(app PRIVATE fmt::fmt)
470+
\end{lstlisting}
471+
\end{block}
472+
\begin{block}{FetchContent (GoogleTest)}
473+
\begin{lstlisting}
474+
include(FetchContent)
475+
FetchContent_Declare(
476+
googletest
477+
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip)
478+
FetchContent_MakeAvailable(googletest)
479+
\end{lstlisting}
480+
\end{block}
481+
\end{frame}
482+
483+
\begin{frame}[fragile]{Testing with CTest and GoogleTest}
484+
Integrate tests and run via CTest.
485+
\begin{block}{CMakeLists.txt}
486+
\begin{lstlisting}
487+
enable_testing()
488+
add_executable(app_tests tests/tests.cpp)
489+
target_link_libraries(app_tests PRIVATE gtest_main)
490+
include(GoogleTest)
491+
gtest_discover_tests(app_tests)
492+
\end{lstlisting}
493+
\end{block}
494+
Run tests:
495+
\begin{lstlisting}[language=bash]
496+
ctest --output-on-failure -j
497+
\end{lstlisting}
498+
\end{frame}
499+
500+
\begin{frame}[fragile]{CMake Presets (CMakePresets.json)}
501+
Shareable configure/build/test profiles for CLI and IDEs.
502+
\begin{block}{CMakePresets.json}
503+
\begin{lstlisting}
504+
{
505+
"version": 3,
506+
"cmakeMinimumRequired": {"major": 3, "minor": 22},
507+
"configurePresets": [
508+
{"name":"ninja-debug","generator":"Ninja",
509+
"binaryDir":"build/debug",
510+
"cacheVariables":{"CMAKE_BUILD_TYPE":"Debug"}}
511+
],
512+
"buildPresets": [
513+
{"name":"build-debug","configurePreset":"ninja-debug"}
514+
],
515+
"testPresets": [
516+
{"name":"test-debug","configurePreset":"ninja-debug"}
517+
]
518+
}
519+
\end{lstlisting}
520+
\end{block}
521+
\begin{lstlisting}[language=bash]
522+
cmake --preset ninja-debug
523+
cmake --build --preset build-debug
524+
ctest --preset test-debug
525+
\end{lstlisting}
526+
\end{frame}
527+
528+
\begin{frame}[fragile]{Cross-compiling and toolchains}
529+
Point CMake to a toolchain file for non-host targets.
530+
\begin{lstlisting}[language=bash]
531+
cmake -S . -B build-arm \
532+
-DCMAKE_TOOLCHAIN_FILE=toolchains/arm-none-eabi.cmake
533+
\end{lstlisting}
534+
\begin{block}{toolchains/arm-none-eabi.cmake}
535+
\begin{lstlisting}
536+
set(CMAKE_SYSTEM_NAME Generic)
537+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
538+
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
539+
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
540+
\end{lstlisting}
541+
\end{block}
542+
\end{frame}
543+
544+
\begin{frame}[fragile]{Install, export, and CPack}
545+
Install targets and generate simple packages.
546+
\begin{block}{CMakeLists.txt}
547+
\begin{lstlisting}
548+
install(TARGETS app EXPORT appTargets RUNTIME DESTINATION bin)
549+
install(EXPORT appTargets NAMESPACE app:: DESTINATION lib/cmake/app)
550+
include(CPack)
551+
\end{lstlisting}
552+
\end{block}
553+
Create an archive package:
554+
\begin{lstlisting}[language=bash]
555+
cpack -G ZIP
556+
\end{lstlisting}
557+
\end{frame}
558+
399559
\begin{frame}{CMake demo}
400560
Demo
401561
\end{frame}

02-cmake/02-cmake.toc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
\beamer@sectionintoc {1}{Building C++ projects}{3}{0}{1}
22
\beamer@sectionintoc {2}{Build systems history}{9}{0}{2}
3-
\beamer@sectionintoc {3}{CMake}{16}{0}{3}
3+
\beamer@sectionintoc {3}{CMake}{17}{0}{3}

0 commit comments

Comments
 (0)