Skip to main content

Mastering Cmake | Pdf

project/ ├── CMakeLists.txt (top-level) ├── cmake/ │ └── FindMyCustomLib.cmake ├── src/ │ ├── CMakeLists.txt │ └── app.cpp ├── libs/ │ ├── core/ │ │ ├── CMakeLists.txt │ │ └── core.cpp │ └── utils/ │ ├── CMakeLists.txt │ └── utils.h (header-only) └── tests/ ├── CMakeLists.txt └── test_core.cpp cmake_minimum_required(VERSION 3.20) project(MyProject VERSION 1.0.0 LANGUAGES CXX) Options option(BUILD_TESTS "Build unit tests" ON) option(BUILD_SHARED_LIBS "Build shared libs instead of static" OFF) Global settings set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) Add subdirectories add_subdirectory(libs/core) add_subdirectory(libs/utils) add_subdirectory(src)

add_executable(my_app main.cpp) target_include_directories(my_app PRIVATE include) target_link_directories(my_app PRIVATE /custom/lib) Why? Target properties don’t pollute sibling directories or subprojects. | Modifier | Effect | |------------|-------------------------------------------------------------------------| | PRIVATE | Used only by this target, not by dependents (e.g., internal .cpp includes) | | PUBLIC | Used by this target and all dependents (e.g., header include dirs) | | INTERFACE | Used only by dependents, not by this target (e.g., header-only libs) |

find_path(MyLib_INCLUDE_DIR mylib.h PATHS /usr/include /usr/local/include) find_library(MyLib_LIBRARY mylib PATHS /usr/lib /usr/local/lib) include(FindPackageHandleStandardArgs) find_package_handle_standard_args(MyLib DEFAULT_MSG MyLib_LIBRARY MyLib_INCLUDE_DIR) mastering cmake pdf

"version": 3, "configurePresets": [ "name": "debug", "displayName": "Debug", "generator": "Ninja", "binaryDir": "$sourceDir/build/debug", "cacheVariables": "CMAKE_BUILD_TYPE": "Debug", "BUILD_TESTS": "ON" , "name": "release", "inherits": "debug", "displayName": "Release", "binaryDir": "$sourceDir/build/release", "cacheVariables": "CMAKE_BUILD_TYPE": "Release" ], "buildPresets": [ "name": "debug", "configurePreset": "debug" , "name": "release", "configurePreset": "release" ]

add_library(math STATIC add.cpp multiply.cpp) target_include_directories(math PUBLIC $CMAKE_CURRENT_SOURCE_DIR/include) target_compile_features(math PUBLIC cxx_std_17) option(BUILD_MATH_EXAMPLES "Build math examples" OFF) if(BUILD_MATH_EXAMPLES) add_executable(calc_example examples/calc.cpp) target_link_libraries(calc_example PRIVATE math) endif() project/ ├── CMakeLists

if(MyLib_FOUND AND NOT TARGET MyLib::MyLib) add_library(MyLib::MyLib UNKNOWN IMPORTED) set_target_properties(MyLib::MyLib PROPERTIES IMPORTED_LOCATION "$MyLib_LIBRARY" INTERFACE_INCLUDE_DIRECTORIES "$MyLib_INCLUDE_DIR" ) endif() 1. Generator Expressions Evaluated during build system generation, not during CMake configuration.

set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_PROCESSOR arm) set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc) set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++) set(CMAKE_FIND_ROOT_PATH /usr/arm-linux-gnueabihf) Usage: cmake -DCMAKE_TOOLCHAIN_FILE=toolchain_arm.cmake .. Allow other projects to find your library via find_package : and testing across teams:

add_executable(test_core test_core.cpp) target_link_libraries(test_core PRIVATE core) add_test(NAME CoreSanity COMMAND test_core) add_test(NAME CoreEdgeCase COMMAND test_core --edge) Presets: The Future of CMake Workflows (3.19+) CMakePresets.json standardizes configuring, building, and testing across teams: