Mastering Cmake: Pdf
:
add_custom_command( OUTPUT $CMAKE_CURRENT_BINARY_DIR/version.h COMMAND $CMAKE_COMMAND -DVERSION=$PROJECT_VERSION -P write_version.cmake DEPENDS write_version.cmake COMMENT "Generating version.h" ) add_custom_target(generate_version DEPENDS $CMAKE_CURRENT_BINARY_DIR/version.h) add_dependencies(my_app generate_version) toolchain_arm.cmake :
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
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:
Introduction CMake is the de facto standard build system generator for C and C++ projects. Unlike traditional build systems (Make, Ninja, Visual Studio), CMake doesn’t build your code directly. Instead, it generates platform-native build scripts that compile and link your software reliably across Linux, macOS, Windows, and embedded systems. add_library(utils STATIC utils
add_library(utils STATIC utils.cpp) target_include_directories(utils PUBLIC include/private) # Both utils and my_app need it target_compile_definitions(utils PRIVATE UTILS_BUILD=1) add_executable(my_app main.cpp) target_link_libraries(my_app PRIVATE utils) # utils's PUBLIC includes propagate A master-level CMake project looks like this:
install(FILES "$CMAKE_CURRENT_BINARY_DIR/MyProjectConfigVersion.cmake" DESTINATION lib/cmake/MyProject ) CMake’s built-in testing is trivial to use: and embedded systems.
# Install targets install(TARGETS my_lib EXPORT MyProjectTargets LIBRARY DESTINATION lib ARCHIVE DESTINATION lib INCLUDES DESTINATION include ) install(DIRECTORY include/ DESTINATION include) install(EXPORT MyProjectTargets FILE MyProjectTargets.cmake NAMESPACE MyProject:: DESTINATION lib/cmake/MyProject ) Write config version file include(CMakePackageConfigHelpers) write_basic_package_version_file( "$CMAKE_CURRENT_BINARY_DIR/MyProjectConfigVersion.cmake" VERSION $PROJECT_VERSION COMPATIBILITY SameMajorVersion )
