I'd think the best commands here are likely to be add_subdirectory
(as you suspected) and target_link_libraries
.
I guess with your directory structure, I'd expect to see a "top-level" CMakeLists.txt in the root. In that CMake file, you'd invoke the subdirectories' CMakeLists using add_subdirectory
.
I imagine both Library1
and Library2
are actual CMake targets, included via add_library
, and similarly you have add_executable(Executable1 ...)
. In this case, you can add the following to Library1/CMakeLists.txt:
target_link_libraries(Library1 Library2)
CMake now automatically links Library2
whenever Library1
is specified as a dependency. If Library2
is modified, then it will be rebuilt automatically before being linked to Library1
again.
Likewise in Executable1/CMakeLists.txt you can then do:
target_link_libraries(Executable1 Library1)
Probably the only thing to watch for here is that the order of the add_subdirectory
commands would need to be
add_subdirectory(Library2)
add_subdirectory(Library1)
add_subdirectory(Executable1)
so that the dependencies are defined before they're referred to in the target_link_libraries
calls.
A final point which seems odd to me is that you have a build directory per target. Normally there should only be a need for a single build directory (preferably outside the source tree).