Scoped Subdirectories
Let's get introduced to the subdirectories and their scope in CMake.
It's a common practice to structure our project following the natural structure of the filesystem, where nested directories represent the discrete elements of the application: the business logic, GUI, API, and reporting, and finally, separate directories with tests, external dependencies, scripts, and documentation.
The add_subdirectory()
command
To support this concept, CMake offers the following command:
add_subdirectory(source_dir [binary_dir][EXCLUDE_FROM_ALL])
As already established, this adds a source directory to our build. Optionally, we may provide a path in which built files will be written (binary_dir
). The EXCLUDE_FROM_ ALL
keyword will disable the default building of targets defined in the subdirectory. This may be useful for separating parts of the project that aren't needed for the core functionality (for example, examples and extensions).
This command will look for a CMakeLists.txt
file in the source_dir
path (evaluated relative to the current directory). This file will then be parsed in the directory scope, meaning that all the ...
Get hands-on with 1400+ tech skills courses.