[CMake] Using add_subdirectory to manage project dependencies that aren't actually in a subdirectory

Joe jtcacct at gmail.com
Fri Sep 18 18:42:15 EDT 2015


Hi.  I was hoping to get some guidance on if this is bad practice.  I am
simply trying to find a way to manage internal dependencies of my project
but with a file structure that is distributed.  It looks like this:

/path/to/A

/path/to/B

B depends on A.  I cannot change this directory structure.  The solution we
have chose is to create the following:

/path/to/modules/FindA.cmake:

...
if (NOT TARGET A)
   set(PATH_TO_A ${CMAKE_CURRENT_LIST_DIR}../A)
   add_subdirectory(${PATH_TO_A} ${PATH_TO_A}/build)
endif()
...

This way, A' CMakeLists is invoked and the library is compiled and built. 
Then in B's CMakeLists.txt:

...
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
   ${CMAKE_CURRENT_LIST_DIR}/../modules)

find_package(A)
add_executable(main_B src/main_B.cpp)
target_link_libraries(main_B A)
...

Is this bad practice?  I tried to figure out find_package in CONFIG mode but
every example I've found seems complicated and obtuse.  Thank you.





More information about the CMake mailing list