Mastering Cmake Pdf -
# Different compile definitions per config target_compile_definitions(my_app PRIVATE $<$<CONFIG:Debug>:DEBUG_BUILD> $<$<CONFIG:Release>:NDEBUG> ) target_link_libraries(my_app PRIVATE $<$<PLATFORM_ID:Windows>:ws2_32> $<$<PLATFORM_ID:Linux>:dl> ) 2. Custom Commands and Targets Generate code or run tools:
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 : mastering cmake pdf
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) | not by dependents (e.g.
"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" ] not by this target (e.g.
# 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 )