How do I use find_package and targets in CMake for C++?

In CMake, the `find_package` command is used to locate external libraries and dependencies, while targets allow you to specify the linked libraries and their usage in a more modern, flexible, and reusable way. Below is a brief explanation and example demonstrating how to use `find_package` and targets in CMake for a C++ project.

cmake_minimum_required(VERSION 3.10) project(MyProject) # Find the package (for instance, Boost) find_package(Boost 1.70 REQUIRED COMPONENTS system filesystem) # Add an executable target add_executable(my_executable main.cpp) # Link Boost libraries to the target target_link_libraries(my_executable PRIVATE Boost::system Boost::filesystem)

CMake find_package targets C++ CMake targets linking libraries external dependencies Boost