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

Raymond Wan rwan.work at gmail.com
Fri Sep 18 23:35:36 EDT 2015


Hi Joe,


On Sat, Sep 19, 2015 at 6:42 AM, Joe <jtcacct at gmail.com> wrote:
> /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.


I'm interested in what you find.  Until now, I've been doing what you
and losif are doing.  The reason is that the top-level program (i.e.,
Z) makes use of A and B.  But also, I want to test B independently of
Z, but I need to link in A to do it.

I'm not sure if this is relevant, but what I've been doing is
following this old thread:
http://www.cmake.org/pipermail/cmake/2010-December/041150.html .  I'm
not sure if a better way is available with the current version of
CMake...  Anyway, perhaps this post will help (it's not from me)?

Ray


More information about the CMake mailing list