[CMake] Specifying code in one subdirectory depends on a library from another subdirectory

Timothy Wrona tjwrona1992 at gmail.com
Tue Mar 19 13:13:54 EDT 2019


I am working on a complex CMake project that is part of a large legacy
system that uses a top level project and "add_subdirectory" to create
subprojects.

I have run into an issue because now one of the subprojects is dependant on
another subproject, but I can't seem to find a clear way to tell CMake
about this dependency.

Consider this example:

    my_project/
        CMakeLists.txt:
          add_subdirectory('subproject1')
          add_subdirectory('subproject2')

    my_project/subproject1/
        CMakeLists.txt:
            add_executable(subproject1_exe <sources>)
            target_link_libraries(subproject1_exe subproject2_lib) # <--
THIS is the problem

    my_project/subproject2/
        CMakeLists.txt:
            add_library(subproject2_lib <sources>)

The actual code is much more complex than this, but this simple example
illustrates the problem. The actual compilation error I am getting is
caused by subproject1 including a header file that gets generated when
subproject2 is built.

Does anyone know how to properly tell CMake about the dependency so it will
build correctly?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190319/32249cd3/attachment.html>


More information about the CMake mailing list