CMake Best Practices
In a Nutshell
Declare modules with
ADD_LIBRARYorADD_EXECUTABLEDeclare build flags with
TARGET_xxx()Declare dependencies with
TARGET_LINK_LIBRARIESSpecify which is
PUBLICand which isPRIVATE
Boilderplate
Headers
cmake_minimum_required(VERSION 3.0)
# declare top-level flags
if (MSVC)
add_compile_options(/W3 /WX)
else()
add_compile_options(-W -Wall -Werror)
endif()
Add Libraries
Declare Flags
Declare Dependencies
Header-only libraries
INTERFACE basically indicates that we don't need to build anything
DO NOT
Don't use macros that affect all targets:
INCLUDE_DIRECTORIES()ADD_DEFINITIONS()LINK_LIBRARIES
Dont' use
TARGET_INCLUDE_LIBRARIES()with path outside your own moduleDon't use
TARGET_LINK_LIBRARIES()without specifying scope (PUBLIC, PRIVATE, or INTERFACE)Don't use
TARGET_COMPILE_OPTIONS()to set flags that affect the ABI
Last updated