DEV Community

Cover image for Series I: CMake & C++17 Quick Started
L Djohari
L Djohari

Posted on

Series I: CMake & C++17 Quick Started

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 and CMakeLists.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 and OOP.
  • Mastering INCLUDE and PROJECT-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, and std::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 and Class 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

  1. Origins (2000): Created by Kitware to support scientific projects like ITK and VTK.
  2. Adoption: Quickly gained traction in open-source and enterprise projects for multi-platform support.
  3. Modern Usage: Used by major projects like LLVM, KDE, Qt, Google Abseil, Google gRPC, and more.

Why Use CMake?

  1. Flexibility in Defining Projects:

    Precisely configure targets, source files, and build steps using CMakeLists.txt. To add nested sub-projects, simply use add_subdirectory(<source-folder> <build-dir>). The CMakeLists.txt file serves as the entry point of the project.

  2. Cross-Platform Support:

    Generates build files for Linux, Windows, and macOS.

  3. Compile Flags Management:

    Easily set compiler flags for standards (e.g., -std=c++17), optimization, warnings, debugging, or application-specific definition flags.

  4. Custom Shell Commands:

    Run shell commands and scripts using add_custom_command or execute_process.

  5. Dependency Management:

    Seamlessly integrates libraries using find_package, FetchContent, or by adding the source folder with add_subdirectory.

  6. IDE Integration:

    Works flawlessly with IDEs like Visual Studio, Visual Studio Code, Xcode, CLion, and Eclipse.

  7. Testing and Installation:

    Built-in support for test frameworks (via CTest) and installation processes.

  8. Modern C++ Features:

    Leverages features like C++11/14/17/20, LTO, and sanitizers.

  9. 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

  1. Java: Gradle, Maven.
  2. Android: Gradle (Android Plugin).
  3. iOS/macOS: Xcodebuild, Swift Package Manager (SPM).
  4. Rust: Cargo.
  5. Python: setuptools, pipenv.
  6. JavaScript/Node.js: npm, yarn.
  7. .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 and CMakeLists.txt configuration.

See you on next series =)

Top comments (4)

Collapse
 
ajeebkp23 profile image
Ajeeb.K.P

Looking for more ❤️

Collapse
 
lwdjohari profile image
L Djohari • Edited

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.

Collapse
 
lovelindhoni profile image
Lovelin

just the post I have been looking for!

Collapse
 
javibarbaran profile image
Javier Barbaran

Great post! Thanks for sharing!