What is a good layout for the installation of your project? As long as you are the only consumer of your project, this question only has limited relevance. However, as soon as you start shipping to the outside world, it will be expected that you provide a sensible layout when installing your project. Fortunately, there are standards that we can adhere to and CMake can help us with that. Effectively, what the GNUInstallDirs.cmake module does is to define a set of variables. These variables are the names of the subdirectories where different types of files should be installed. In our example we used the following:
- CMAKE_INSTALL_BINDIR: This will give the subdirectory where user executables should be located, which is the bin directory under the chosen install prefix.
- CMAKE_INSTALL_LIBDIR: This expands to the subdirectory where object code libraries – that is, the static and shared libraries – should be located. On a 64-bit system, this is lib64, whereas on a 32-bit system, it is just lib.
- CMAKE_INSTALL_INCLUDEDIR: Finally, we used this variable to obtain the correct subdirectory for our C header files. This variable expands to include.
The user might, however, want to override these choices. We allowed for that with the following stanza in the root CMakeLists.txt file:
# Offer the user the choice of overriding the installation directories
set(INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Installation directory for libraries")
set(INSTALL_BINDIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Installation directory for executables")
set(INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Installation directory for header files")
This effectively redefines the INSTALL_BINDIR, INSTALL_LIBDIR, and INSTALL_INCLUDEDIR convenience variables to be used within our project. We also define the additional INSTALL_CMAKEDIR variable, but its role will be discussed in detail in the next few recipes.