Chapter 2. Setting Up A Project
Without a build system, a project is just a collection of files. CMake brings some order to this,
starting with a human-readable file called CMakeLists.txt that defines what should be built and how,
what tests to run and what package(s) to create. This file is a platform-independent description of
the whole project, which CMake then turns into platform specific build tool project files. As its
name suggests, it is just an ordinary text file which developers edit in their favorite text editor or
development environment. The contents of this file are covered in great detail in subsequent
chapters, but for now, it is enough to know that this is what controls everything that CMake will do
in setting up and performing the build.
A fundamental part of CMake is the concept of a project having both a source directory and a
binary directory. The source directory is where the CMakeLists.txt file is located and the project’s
source files and all other files needed for the build are organized under that location. The source
directory is frequently under version control with a tool like git, subversion, or similar.
The binary directory is where everything produced by the build is created. It is often also called the
build directory. For reasons that will become clear in later chapters, CMake generally uses the term
binary directory, but among developers, the term build directory tends to be in more common use.
This book tends to prefer the latter term since it is generally more intuitive. CMake, the chosen
build tool (e.g. make, Visual Studio, etc.), CTest and CPack will all create various files within the build
directory and subdirectories below it. Executables, libraries, test output and packages are all
created within the build directory. CMake also creates a special file called CMakeCache.txt in the build
directory to store various information for reuse on subsequent runs. Developers won’t normally
need to concern themselves with the CMakeCache.txt file, but later chapters will discuss situations
where this file is relevant. The build tool’s project files (e.g. Xcode or Visual Studio project files,
Makefiles, etc.) are also created in the build directory and are not intended to be put under version
control. The CMakeLists.txt files are the canonical description of the project and the generated
project files should be considered part of the build output.
When a developer commences work on a project, they must decide where they want their build
directory to be in relation to their source directory. There are essentially two approaches: in-source
and out-of-source builds.
2.1. In-source Builds
It is possible, though discouraged, for the source and build directories to be the same. This
arrangement is called an in-source build. Developers at the beginning of their career often start out
using this approach because of the perceived simplicity. The main difficulty with in-source builds,
however, is that all the build outputs are intermixed with the source files. This lack of separation
causes directories to become cluttered with all sorts of files and subdirectories, making it harder to
manage the project sources and running the risk of build outputs overwriting source files. It also
makes working with version control systems more difficult, since there are lots of files created by
the build which either the source control tool has to know to ignore or the developer has to
manually exclude during commits. One other drawback to in-source builds is that it can be non-
trivial to clear out all build output and start again with a clean source tree. For these reasons,
developers are discouraged from using in-source builds where possible, even for simple projects.
6