Targets, Properties, And More

Target Types

  • Two basic types: ADD_EXECUTABLE() and ADD_LIBRARY()

  • ADD_EXECUTABLE() is not relevant …

    • Only entry point into dependency graph

    • ⟶ does not propagate anything to dependers

    • There are no dependers

  • ADD_LIBRARY()

    Type

    Description

    Normal library

    STATIC (default), SHARED, MODULE. Has sources to be built.

    Object libraries

    Technically much like normal libraries, but not an archive or shared object (only virtual, implemented by CMake)

    Interface libraries

    No sources; only there to propagate properties to dependers, and to have further dependencies on their own.

Properties

(For a complete list see ADD_LIBRARY())

Target function

Property name

Description

Documentation

TARGET_COMPILE_DEFINITIONS()

COMPILE_DEFINITIONS

Macros set on the compiler command line (-Dname=value)

TARGET_COMPILE_DEFINITIONS()

TARGET_COMPILE_OPTIONS()

COMPILE_OPTIONS

Non-macro compiler flags/options

TARGET_COMPILE_OPTIONS()

TARGET_INCLUDE_DIRECTORIES()

INCLUDE_DIRECTORIES

Include directories

TARGET_INCLUDE_DIRECTORIES()

TARGET_LINK_LIBRARIES()

(Much more complicated, see documentation)

Dependencies, in the widest sense

TARGET_LINK_LIBRARIES()

Properties: PRIVATE, PUBLIC, INTERFACE?

  • PUBLIC propagated to dependers

    • Dependency through #include <other.h> in a header file ⟶ all includers need to know

    • blacklist availability in approach 2 (but only if blacklist has compiled code)

    • PUBLIC only possible when target has compiled code

  • PRIVATE does not propagate

    • For example, one might structure a target’s source code into src/, private-inc/, and public-inc/private-inc/ would be TARGET_INCLUDE_DIRECTORIES(... PRIVATE ...)

    • For example, TARGET_COMPILE_DEFINITIONS() for target-local compilation only

  • INTERFACE propagated to dependers

    • Just like PUBLIC - except that PUBLIC is not possible on INTERFACE targets (e.g. header-only libraries)

    • Asymmetric; smells like it does in many corners of CMake

    • Documentation has no clear explanation. Exceptions, and paragraph-long explanations.