# Dependency A-\>B-\>C

**URL:** <https://discourse.cmake.org/t/dependency-a-b-c/11061>\
**Category:** Code\
**Created:** [June 19, 2024, 11:42pm UTC](https://discourse.cmake.org/t/dependency-a-b-c/11061 "2024-06-19T23:42:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![helloworld](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@helloworld](https://discourse.cmake.org/u/helloworld)\
**Post date:** [June 19, 2024, 11:42pm UTC](https://discourse.cmake.org/t/dependency-a-b-c/11061/1 "2024-06-19T23:42:17Z")

</div>

Hello,

I am reading about target\_link\_libraries and the PUBLIC contract. I have this specific situation:

project/  
├── CMakeLists.txt (1)  
├── dir1/  
│ ├── subdir1/  
│ │ └── CMakeLists.txt (2)  
│ └── src/  
│ └── main.c

In CMakeLists.txt (1), I just do this:  
add\_subdirectory(“dir1/subdir1/”)

In CMakeLists.txt (2), I am pulling an external\_project:

include(ExternalProject)  
ExternalProject\_Add(projectA  
GIT\_REPOSITORY   
GIT\_TAG   
CMAKE\_ARGS  
-DCMAKE\_INSTALL\_PREFIX=“new-build/deps”  
-DCMAKE\_BUILD\_TYPE=Release  
BUILD\_ALWAYS TRUE  
GIT\_PROGRESS TRUE  
TEST\_COMMAND “”  
)

set(gen\_libs libA libB) # these are coming from projectA  
set(srcs  
src/main.c)  
add\_executable(test ${srcs})  
add\_dependencies(test projectA)  
target\_include\_directories(test PRIVATE new-build/deps/include)  
target\_link\_directories(test PRIVATE new-build/deps/lib)  
target\_link\_libraries(test PRIVATE gen\_libs)

include(GNUInstallDirs)

install(TARGETS test  
RUNTIME DESTINATION ${CMAKE\_INSTALL\_FULL\_BINDIR})

In project A, I am essentially generating libA and libB. Each of these libs depends on other libraries that are generated in projectA dependencies. However, nothing from libA and libB dependencies is directly used in test. If I do not explicitly link the sub dependencies to “test” target, the build fails with undefined references.

Note that I do see all the relevant libs in the new-build/deps/lib and that is the only folder where it exists. The sub-dependencies of libA and libB are linked as:

target\_link\_libraries(  
libA  
PUBLIC  
libC)  
target\_link\_libraries(  
libB  
PUBLIC  
libC  
libD)

install(TARGETS libA libB  
ARCHIVE DESTINATION ${CMAKE\_INSTALL\_LIBDIR}  
LIBRARY DESTINATION ${CMAKE\_INSTALL\_LIBDIR}  
RUNTIME DESTINATION ${CMAKE\_INSTALL\_BINDIR}  
)

install(DIRECTORY ${src}/src/include/  
DESTINATION include  
)

What am I missing? I thought PUBLIC handles transitive dependencies but maybe I am misunderstanding it. How do I build test executable without having to link to libC and libD? Note that to build, I am creating `build` directory in root project directory and running cmake … inside that
