Search code examples
c++linuxcmakeinstallation

CMake: How to specify the target on which "install" depends?


As far as I have realized, the "INSTALL" command by default depends on all available targets. How can I configure the cmake file so that "install" depends only on a specific target?


Solution

  • The install target created by CMake depends on the all target, which, as its name suggests, in turn depends on every other (non-custom) target in the project.

    You can exclude targets from all by using the EXCLUDE_FROM_ALL target property:

    add_executable(foo EXCLUDE_FROM_ALL ${FOO_SOURCES})
    

    Be aware though that:

    Installing a target with the EXCLUDE_FROM_ALL target property set to TRUE has undefined behavior.

    Quoted from the INSTALL manpage.

    While you can manually suppress the dependency from install to all by setting the variable CMAKE_SKIP_INSTALL_ALL_DEPENDENCY, doing so is not useful in this case, as it is not possible to manually add additional dependencies to install.