# CMake: Common dependency between different projects

**URL:** <https://discourse.cmake.org/t/cmake-common-dependency-between-different-projects/2593>\
**Category:** Code\
**Created:** [January 20, 2021, 10:22pm UTC](https://discourse.cmake.org/t/cmake-common-dependency-between-different-projects/2593 "2021-01-20T22:22:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmylon](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pmylon](https://discourse.cmake.org/u/pmylon)\
**Post date:** [January 20, 2021, 10:22pm UTC](https://discourse.cmake.org/t/cmake-common-dependency-between-different-projects/2593/1 "2021-01-20T22:22:18Z")

</div>

Hello!

I have a question regarding a project (C++, CMake) I am working on. Let’s assume the following simplified project structure for demonstration purposes (each folder contains some source and header files):

demo\_project  
|  
|–hal  
|  
|–util

Both demo\_project and hal depend on util. For now I am using a simple Modern CMake approach where I use target\_sources so that I “push” the dependencies on the target demo\_project.

/\* Example of util’s CMakeLists.txt \*/  
target\_sources(demo\_project  
PUBLIC  
#some util files  
PRIVATE  
#some util files  
)

With this approach, hal does not need to take into cosideration that it needs some files from util in its CMakeLists.txt.

The key idea now is that hal could be later built as an independent library (a standalone project) which could be used in different projects. So I would like a project structure like the following:

demo\_project  
|  
|–hal  
| |  
| |–util  
|  
|–util

So that I can later take hal and incorporate it in a different project which may not depend on util.

new\_project  
|  
|–hal  
| |  
| |–util  
|  
|–foo

The problem is that I don’t want demo\_project which depends on util, and hal to use a separate copy of util. I want them both to use the same copy of util.

I cannot leave the util folder only inside hal. The problem with this approach is that if for some reason hal was replaced by another implementation, e.g. hal2 which did not depend on util, the project would not built.

I could make the assumption that in both cases util is placed in the same level with hal. In this case for  
a project (let’s call it demo\_project) that depends on util, I would have to do the following:  
/\* CMakeLists.txt of demo\_project \*/  
add\_subdirectory(hal)  
add\_subdirectory(util)  
…

/\* CMakeLists.txt of hal \*/  
add\_subdirectory(…/util)  
…

But this approach seems a little bit “ugly” to say the least. And I am not sure what happens in this case, that both demo\_project and hal add the same CMakeLists.txt file.

Could you provide any feedback or suggest an alternative better approach?

Disclaimer: I am new to CMake, hope I described my problem well! Let me know if you need more info! Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [January 21, 2021, 3:19pm UTC](https://discourse.cmake.org/t/cmake-common-dependency-between-different-projects/2593/2 "2021-01-21T15:19:15Z")

</div>

`hal` can ship its own `util`, but it should have an option to say "I brought my own `util`" and give it the information it needs to consume it (basically the targets I assume). The combining project(s) can then just set up `hal` to build to match its structure.

Cc: @craig.scott
