# creating a standalone STATIC/SHARED top level target from other targets

**URL:** https://discourse.cmake.org/t/creating-a-standalone-static-shared-top-level-target-from-other-targets/14112
**Category:** Usage
**Created:** [May 17, 2025, 12:32am UTC](https://discourse.cmake.org/t/creating-a-standalone-static-shared-top-level-target-from-other-targets/14112 "2025-05-17T00:32:14Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![cargo\_run](https://discourse.cmake.org/user_avatar/discourse.cmake.org/cargo_run/32/5571_2.png) [@cargo\_run](https://discourse.cmake.org/u/cargo_run)
#### Post date: [May 17, 2025, 12:32am UTC](https://discourse.cmake.org/t/creating-a-standalone-static-shared-top-level-target-from-other-targets/14112/1 "2025-05-17T00:32:14Z")

</div>

`cmake_minimum_required(VERSION 3.10.2)`

hey, I was wondering what is the best way to achieve the following goals (given the folder structure below):

- create a shared/static lib from each sub project, `sdk::a` and `sdk::b`. Ideally not generated unless used explicitly.
- create tests executables for each sub project that was generated.
- optionally create a top level standalone target `sdk::sdk` (static/shared).
- if `sdk::sdk` is created aggregate the test cases into a single `sdk::sdk` test.

I’ve considered adding addition object targets (I.e `sdk::a_obj` `sdk::a_test_obj` ) or aggregating the sources/includes in a list, which is better? are the any better alternatives?

is there a way to conditionally generate the sub projects and their tests without using flags everywhere? `add_sub_directory("${path}" EXCLUDE_FROM_ALL)` no longer holds because the file is used by `sdk::sdk`.

is there a way to configure the targets to be `SHARED`/`STATIC`/`OBJECT` sperately from the top level using cmake builtin functions? could it be done using the alias?

```auto
sdk/
├── cmake/
├── externals/
├── sdk/
│ ├── a/
│ │ ├── include/
│ │ │ └── sdk/
│ │ │ └── a.hpp
│ │ ├── src/
│ │ │ └── a.cpp
│ │ ├── tests/
│ │ │ └── test_a.cpp
│ │ └── CMakeLists.txt
│ ├── b/
│ │ ├── include/
│ │ │ └── sdk/
│ │ │ └── b.hpp
│ │ ├── src/
│ │ │ └── b.cpp
│ │ ├── tests/
│ │ │ └── test_b.cpp
│ │ └── CMakeLists.txt
│ └── CMakeLists.txt
└── CMakeLists.txt

```
