How do I write modern target-based CMake in CMake for C++?

Modern CMake simplifies the process of building C++ projects by using a target-based approach. This means that instead of manually specifying include directories and source files for each target, you can create and manage targets more easily, resulting in cleaner and more maintainable CMake files.

Here is a basic example of how to write a simple CMakeLists.txt using modern target-based CMake:

cmake_minimum_required(VERSION 3.10) # Define the project name project(MyProject) # Create a library target add_library(MyLibrary src/mylibrary.cpp) # Specify include directories for the library target_include_directories(MyLibrary PUBLIC include) # Create an executable target add_executable(MyExecutable src/main.cpp) # Link the library to the executable target_link_libraries(MyExecutable PRIVATE MyLibrary)

modern CMake target-based approach C++ CMakeLists.txt project organization