...

/

The Concept of a Target

The Concept of a Target

Let's learn about some general target concepts.

If you have ever used GNU Make, you will have already seen the concept of a target. Essentially, it's a recipe that a buildsystem uses to compile a list of files into another file. It can be a .cpp implementation file compiled into an .o object file, a group of .o files packaged into an .a static library, and many other combinations.

CMake, however, allows us to save time and skip the intermediate steps of those recipes; it works on a higher level of abstraction. It understands how to build an executable directly from source files. So, we don't need to write an explicit recipe to compile object files. All that's required is an add_executable() command with the name of the executable target and a list of .cpp files (it can consist of a single .cpp file) that are to be its elements:

add_executable(app1 a.cpp b.cpp c.cpp)

We already used this command and know how executable targets are used in practice—during the generation step, CMake will create a buildsystem and fill it with recipes to compile each of the source files and link them together into a single binary executable.

In CMake, we can create a target using one of three commands:

  • add_executable()

  • add_library()

  • add_custom_target()

The first two are pretty self-explanatory; we already used them to build executables and libraries. But what are those custom targets?

Custom targets

They allow to specify our own command line that will be executed without checking ...