I have some projects that follows this dependency graph:
Executable
Main
Libraries A, B, and C
Main //_Depends_A_B_C
A //_Depends_B
B
C //Depends_B
Does it make more sense for me to make a super project that uses add_subdirectory
to put A, B, and C in global scope and link them all together?
Proj/
CMakeList.txt // Has add_subdirectory for each directory in b,a,c,Main order.
a/
b/
c/
Main/
Or should I go through the trouble of making a find_package
solution for them.
Main/
A/ //Installed to system
B/ //Installed to system
C/ //Installed to system
These libraries are in development so it is nice to be able to treat them all as one project in my IDE, but I don't want to be misusing add_subdirectory
to fit my specific dev tools.
If the dependency is third-party, then you should use find_package
. This allows someone to replace your dependencies with system-provided ones instead. This is important when authoring a library that might be included in a package manager, like vcpkg or a Linux distro's standard repositories.
If the dependency might be cross-compiled relative to your code (eg. it is a build-time tool or code generator like Bison), then you must use find_package
. Only one toolchain may be active during a given CMake build.
If you store dependencies in your repository (either directly or as submodules), then a super-build setup using ExternalProject
will likely be convenient. Your top-level project would consist of solely external project orchestration, and each sub-project would install to a local prefix, and look for packages in that same prefix. See CMAKE_INSTALL_PREFIX
and CMAKE_PREFIX_PATH
for more detail.
In any other case, prefer add_subdirectory
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With