Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use add_subdirectory or find_package?

Tags:

cmake

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.

like image 756
user514156 Avatar asked Jun 28 '16 16:06

user514156


1 Answers

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.

like image 161
Alex Reinking Avatar answered Nov 09 '22 12:11

Alex Reinking