With CMake, library dependencies are transitive, so you should not call add_subdirectory twice in test/CMakeFiles.txt (and you do not need to list lib1 as a test dependency, since it is already a lib2 's dependency).
So you can change test CMakeFiles.txt to:
cmake_minimum_required(VERSION 2.8.7)
In addition, you should probably delete cmake_minimum_required calls from your CMakeFiles.txt project files that are not project related (lib). For more information, run:
cmake --help-policy CMP0000
This setting will still lead to recompilation of all libs libraries if you add a similar subdirectory test2 and a project that depends on lib1 and lib2 . If you really do not want to have top-level CMakeFiles.txt in projects/ , then you are stuck in what you are doing, or you can use either the export or install command.
export will create a file that can be include d by other projects and which imports targets into the project that calls include .
install can install libraries in another common projects/ subdirectory. Depending on the structure of the source directory, this may be advantageous only so that the intended library API headers are available for dependent projects.
However, both of these options require that dependent library projects be rebuilt (and installed) if they are changed, while your current setup includes all dependent objects in your project, so any change to the source file in the dependent library will cause your object test get tired.
For more information about export and install run the following command:
cmake --help-command export cmake --help-command install
source share