# Share targets between src and tests subdirectory

**URL:** https://discourse.cmake.org/t/share-targets-between-src-and-tests-subdirectory/10564
**Category:** Code
**Created:** [April 4, 2024, 9:11am UTC](https://discourse.cmake.org/t/share-targets-between-src-and-tests-subdirectory/10564 "2024-04-04T09:11:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mredenti](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mredenti/32/4464_2.png) [@mredenti](https://discourse.cmake.org/u/mredenti)
#### Post date: [April 4, 2024, 9:11am UTC](https://discourse.cmake.org/t/share-targets-between-src-and-tests-subdirectory/10564/1 "2024-04-04T09:11:53Z")

</div>

How can I make use of already compiled targets in another subdirectory? Do I necessarily need to build a library?

Suppose I have a (mini) finite element code and have structured the project as follows

│── CMakeLists.txt  
│── unit-tests  
│ └── CMakeLists.txt  
│ └── test\_mesh.cpp  
│ └── test\_element.cpp  
│ └── test\_solver.cpp  
│── src  
└── CMakeLists.txt  
└── mesh.cpp/.hpp  
└── element.cpp/.hpp  
└── solver.cpp/.hpp  
└── main.cpp

The src/CMakeLists.txt files will have a main target composed of mesh.cpp, node.cpp, solver.cpp, element.cpp. I also have a unit-test/CMakeLists.txt that unit tests the mesh, element and solver components. How to avoid having to recompile the same mesh.cpp, node.cpp, solver.cpp, element.cpp also in the unit-test subdirectory? I do not want to define them as targets again but rather be able to use the targets already defined in src/CMakeLists.txt

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [April 4, 2024, 3:42pm UTC](https://discourse.cmake.org/t/share-targets-between-src-and-tests-subdirectory/10564/2 "2024-04-04T15:42:29Z")

</div>

The simplest solution is to make a STATIC library that your executable links.

```auto
# src/CMakeLists.txt
add_library(foo STATIC)
target_sources(foo PRIVATE
    mesh.cpp
    element.cpp
    solver.cpp
)
target_include_directories(foo PUBLIC .)

add_executable(bar)
target_sources(bar PRIVATE main.cpp)

target_link_libraries(bar PRIVATE foo)

```

```auto
# unit-tests/CMakeLists.txt
add_executable(tests)
target_sources(tests PRIVATE
    test_mesh.cpp
    test_element.cpp
    test_solver.cpp
)

target_link_libraries(tests PRIVATE foo)

```

---

<div class="post-metadata">

### Author: ![mredenti](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mredenti/32/4464_2.png) [@mredenti](https://discourse.cmake.org/u/mredenti)
#### Post date: [April 4, 2024, 3:45pm UTC](https://discourse.cmake.org/t/share-targets-between-src-and-tests-subdirectory/10564/3 "2024-04-04T15:45:50Z")

</div>

Ok, thank you. I have two follow up questions:

- Should I make them imported targets or imported targets should left only for dependencies?
- What would be a non-simplest solution?

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [April 4, 2024, 4:05pm UTC](https://discourse.cmake.org/t/share-targets-between-src-and-tests-subdirectory/10564/4 "2024-04-04T16:05:18Z")

</div>

> Should I make them imported targets or imported targets should left only for dependencies?

None of what you are showing requires imported targets at all.

> What would be a non-simplest solution?

I’d just encourage looking into using ctest for running your unit tests.

This blog gives a good introduction to ctest: [Using CMake’s CTest and add\_test To Run All Your Tests](https://matgomes.com/cmake-ctest-to-add-cpp-tests/)

---

<div class="post-metadata">

### Author: ![mredenti](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mredenti/32/4464_2.png) [@mredenti](https://discourse.cmake.org/u/mredenti)
#### Post date: [April 5, 2024, 1:49pm UTC](https://discourse.cmake.org/t/share-targets-between-src-and-tests-subdirectory/10564/5 "2024-04-05T13:49:40Z")

</div>

> [@buildSystemPerson](#):
>
> I’d just encourage looking into using ctest for running your unit tests.
> 
> This blog gives a good introduction to ctest: [Using CMake’s CTest and add\_test To Run All Your Tests](https://matgomes.com/cmake-ctest-to-add-cpp-tests/)

The blog follows the same approach you mentioned of defining a library. It does not really use an alternative approach. It only adds the use of CTest and GTest to the mix but that I am already doing.

Thus, in essence it seems the approach is to define a library
