Caio Wakamatsu

CMake from scratch / Chapter 3

Targets

Everything in CMake is called a target. An executable is a target, a library you use is a target. Understanding targets is foundational to learning how to use CMake

What is a target?

A target in CMake is a name for a definition on how to build code. For example, building off our previous CMakeLists.txt

add_executable(hello main.cpp)

We tell CMake to add an executable, called hello with the file main.cpp. This means that the executable name, and target name, will be hello.

What can you do with a target?

The two big things you can do with targets are

  1. Build: This is what you typically do day to day, when you call cmake --build build --target <name>.
  2. Depend: Dependencies (Which we’ll get to in the next chapter) are also targets. With CMake, you effectively go “make target X depend on target Y”.