Starting modern C++ development is quite a challenging journey. This series aims to demystify Modern C++17 Development with CMake Project.
I'd like to thanks to my mentor back in 2004, if I didn't met my mentor back in 2004 I might be wouldn't knows best-practices on how to develop cross-platform C++.
From him i got invaluable hands-on experiences to use right approaches & right tools for right jobs, and one of them is CMake.
Series I: CMake & C++17 Quick Started
In this first part of the series, we will focus on:
- C++17 Key Features
- Understanding
CMake
as a C++ Project Build Tool.
You can skip to next series to get down to business quick.
In Next Series, we will cover:
- Installing the prerequisite software to develop and compile Modern C++17 with CMake on Linux/WSL2 (Windows Subsystem for Linux 2).
- Integrating Visual Studio Code (VS Code) for CMake C++17 Project Development.
- Writing
C++17
application andCMakeLists.txt
configuration.
Fast-Tracking for Mastering C++17 Development
These are few key-points to fast-tracking your skill to mastering C++17 Development.
The most crucial is the memory management part, once you are mastering this part you can write safe-memory code and feel comfortable with C++.
- Understand CMake for C++17 Project.
- Mastering Data Types, Value Categories and Object Management—Reference, Pointer, Lvalue, Rvalue, and Move Semantic.
- Mastering RAII Principle and Memory Management using smart-pointer.
- Mastering
struct
,class
andOOP
. - Mastering
INCLUDE
andPROJECT-STRUCTURE
. - Understanding static lib, shared lib and executable compilation.
Why C++17
C++17, finalized in 2017, is a significant evolution in the C++ language. Building on its legacy since 1985, it introduced key features that separate it from predecessors, focusing on productivity, safety, and modern programming needs.
C++17 stands out for higher productivity, high performance without Garbage Collector (GC), combining cutting-edge features with backward compatibility, ensuring smooth transitions while enabling modern development practices.
Key Features of C++17
- RAII: RAII (Resource Acquisition Is Initialization) is a C++ design principle where resource management (e.g., memory, file handles) is tied to object lifetime. Resources are acquired in the constructor and released in the destructor, ensuring automatic cleanup, even in case of exceptions.
-
Smart Pointers: Improved management of dynamic memory with -
std::shared_ptr
,std::unique_ptr
, andstd::weak_ptr
. Say goodbye to manual memory allocation & de-allocation. -
std::string: Yes, C++ has
string
object. With modern C++ you will be almost never touch*char
to handle string. -
Generic Programming through Templates:
Type Templates
,Template Specialization
,Variadic Templates
andClass Template Argument Deduction (CTAD)
offering capabilities comparable to languages like Java, C#, Go, or Rust. - Threading Enhancements: Added parallel execution and better concurrency - with standard library features.
- std::optional: Represents optional values to reduce null pointer risks.
- std::variant: Type-safe union for storing predefined types.
- std::filesystem: Cross-platform API for file system operations.
-
Parallel Algorithms: Enables parallel execution policies (e.g., -
std::for_each
). - if constexpr: Compile-time branching for cleaner template code. These allow more complex computations to be performed at compile-time, improving runtime performance.
-
Improved Lambda Functions: Support for
*this
capture and default arguments. - Fold Expressions: Simplifies variadic template argument operations.
- std::string_view: Lightweight, non-owning string references for efficiency.
-
Container Improvements: Enhanced containers like
std::map::try_emplace
and insertion-order guarantees. - Allocator Flexibility: Easy replacement of memory allocators for optimized performance.
CMake as a C++ Build Tool
CMake excels as a flexible and powerful build system generator. It allows developers to define C++ projects precisely, configure compiler settings, manage dependencies, and even execute shell commands—all while generating platform-specific build files like Makefiles, Ninja, or Visual Studio projects.
CMake’s adaptability has made it the go-to tool for modern C++17 development, comparable to Gradle, Maven, or Xcodebuild in other ecosystems.
What is CMake?
CMake enables developers to define every aspect of a C++ project, from build targets and compiler options to testing and installation rules.
It generates build files for tools like Ninja, abstracting platform-specific complexities.
History
- Origins (2000): Created by Kitware to support scientific projects like ITK and VTK.
- Adoption: Quickly gained traction in open-source and enterprise projects for multi-platform support.
- Modern Usage: Used by major projects like LLVM, KDE, Qt, Google Abseil, Google gRPC, and more.
Why Use CMake?
Flexibility in Defining Projects:
Precisely configure targets, source files, and build steps usingCMakeLists.txt
. To add nested sub-projects, simply useadd_subdirectory(<source-folder> <build-dir>)
. TheCMakeLists.txt
file serves as the entry point of the project.Cross-Platform Support:
Generates build files for Linux, Windows, and macOS.Compile Flags Management:
Easily set compiler flags for standards (e.g.,-std=c++17
), optimization, warnings, debugging, or application-specific definition flags.Custom Shell Commands:
Run shell commands and scripts usingadd_custom_command
orexecute_process
.Dependency Management:
Seamlessly integrates libraries usingfind_package
,FetchContent
, or by adding the source folder withadd_subdirectory
.IDE Integration:
Works flawlessly with IDEs like Visual Studio, Visual Studio Code, Xcode, CLion, and Eclipse.Testing and Installation:
Built-in support for test frameworks (viaCTest
) and installation processes.Modern C++ Features:
Leverages features like C++11/14/17/20, LTO, and sanitizers.Extensibility & Reusability:
Highly customizable with user-defined macros, variables, functions, and modules without get knocking-hard with complexity like another build tools.
Equivalent Tools in Other Ecosystems
- Java: Gradle, Maven.
- Android: Gradle (Android Plugin).
- iOS/macOS: Xcodebuild, Swift Package Manager (SPM).
- Rust: Cargo.
- Python: setuptools, pipenv.
- JavaScript/Node.js: npm, yarn.
- .NET: MSBuild, dotnet CLI.
In Next Series, we will cover:
- Installing the prerequisite software to develop and compile Modern C++17 with CMake on Linux/WSL2 (Windows Subsystem for Linux 2).
- Integrating Visual Studio Code (VS Code) for CMake C++17 Project Development.
- Writing
C++17
application andCMakeLists.txt
configuration.
See you on next series =)
Top comments (4)
Looking for more ❤️
This weekend we will upload how to prepare dev environment, cmake, compiler, gdb, vs code and do simple configuration for c++ project. Hopefully if we can add some more about shared libs and static libs compile too.
just the post I have been looking for!
Great post! Thanks for sharing!