# Link test to target being tested?

**URL:** https://discourse.cmake.org/t/link-test-to-target-being-tested/6772
**Category:** Usage
**Created:** [November 2, 2022, 10:36am UTC](https://discourse.cmake.org/t/link-test-to-target-being-tested/6772 "2022-11-02T10:36:59Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![RedDwarf](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@RedDwarf](https://discourse.cmake.org/u/RedDwarf)
#### Post date: [November 2, 2022, 10:36am UTC](https://discourse.cmake.org/t/link-test-to-target-being-tested/6772/1 "2022-11-02T10:36:59Z")

</div>

I have a project with multiple optional components and, sometimes, two of those components have code in common. So we have a structure like this

- lib/common
- src/comp1
- src/comp2
- src/comp3

With let’s say lib/common used by comp1 and comp2.

I don’t want to spend time building lib/common for a platform using only comp3. The solution seems to be to use the EXCLUDE\_FROM\_ALL directory property in lib/.  
This would all be good, but I may have unit tests for lib/common and, since I’m now using EXCLUDE\_FROM\_ALL they are not being built.

So I want a way to build the tests under lib/common when something depending on the “main” target in lib/common is built.  
I guess this could be

- Declaring a target the “main directory target”. When that target build is triggered, all the other targets in that directory are also triggered / the effect of EXCLUDE\_FROM\_ALL disappears?
- Having a way to say “this test is testing this other target” so that, when the target is built, the test is also built.

There is any way to handle this situation?
