Issue
I have a library that is brought in as an external to several applications and included using add_subdirectory
. It has four build flavours due to the flexibility of its use: GUI/CLI and shared/static. GUI apps tend to build the GUI flavour as then the user warnings it produces appear as pop-up dialogs. However it is also used by some command line apps which use the CLI version that produces user warnings on the terminal. Some applications use the shared version because they use other libraries that use this same library, and need shared resources such as output logfile handles. Some applications use the static version because they are small utilities that we want to distribute as standalone binaries without dependencies.
If I am building a CMake project that only uses one of these four flavours, I really only want to build that one single flavour of the library. Yet despite this CMake insists on building all four, which is a total waste of time.
How can I flag to CMake that the library should only be built if it is explicitly listed as a dependency of something else that is being built?
Solution
For each target, set the EXCLUDE_FROM_ALL
property to TRUE
in the library's CMakeLists.txt. It will then only be built if actually required.
set_target_properties(MyLib PROPERTIES
EXCLUDE_FROM_ALL TRUE
)
Answered By - AlastairG