# One CMakeLists.txt file creating multiple relocatable install packages.

**URL:** https://discourse.cmake.org/t/one-cmakelists-txt-file-creating-multiple-relocatable-install-packages/11715
**Category:** Usage
**Created:** [September 18, 2024, 12:06am UTC](https://discourse.cmake.org/t/one-cmakelists-txt-file-creating-multiple-relocatable-install-packages/11715 "2024-09-18T00:06:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)
#### Post date: [September 18, 2024, 12:06am UTC](https://discourse.cmake.org/t/one-cmakelists-txt-file-creating-multiple-relocatable-install-packages/11715/1 "2024-09-18T00:06:43Z")

</div>

I have a CMake project that is something like:

```auto
top
  CMakeLists.txt
  +---foo
  | CMakeLists.txt
  | source/header files
  +---bar
        CMakeLists.txt
        source/header files

```

In this configuration the `foo` folder creates a CMake library name foo and has relocatable CMake package files, and header files.

The folder `bar` is the same, except for a library named bar.

The `top/CMakeLists.txt` is pretty much a helper that just does `add_subdirectory()` on `foo` and `bar`

The problem I’m having is with install. When I run install I want this to go to:

```auto
<CMAKE_INSTALL_PREFIX>
  +--foo
  | +--cmake
  | +--include
  | +--lib
  +--bar
      +--cmake
      +--include
      +--lib

```

But I cannot figure out how to have the foo and bar sub-folders.

My desire is to run

```auto
cd <top>
cmake -B Build .
cmake --build Build --target install

```

And foo and bar are compiled and then installed to the CMAKE\_INSTALL\_PREFIX folder.

---

<div class="post-metadata">

### Author: ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)
#### Post date: [September 19, 2024, 10:29pm UTC](https://discourse.cmake.org/t/one-cmakelists-txt-file-creating-multiple-relocatable-install-packages/11715/2 "2024-09-19T22:29:57Z")

</div>

I kept reading and digging and figured it out.

[This install page](https://cmake.org/cmake/help/book/mastering-cmake/chapter/Install.html) was really helpful.

In short, I need to add `foo` or `bar` as part of the relative install paths for all the various install functions. I was thinking there might be a directory level setting that would work for this, but it’s a per install setting such as

```auto
install(TARGETS foo
    EXPORT foo_Targets
    LIBRARY DESTINATION "foo/bin"
    ARCHIVE DESTINATION "foo/lib"
    RUNTIME DESTINATION "foo/bin"
    PUBLIC_HEADER DESTINATION "foo/include")

install(EXPORT foo_Targets FILE "FooTargets.cmake" DESTINATION "foo/cmake")
install(FILES "${PROJECT_BINARY_DIR}/FooConfig.cmake" DESTINATION "foo/cmake")

```

---

<div class="post-metadata">

### Author: ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)
#### Post date: [September 20, 2024, 12:52am UTC](https://discourse.cmake.org/t/one-cmakelists-txt-file-creating-multiple-relocatable-install-packages/11715/3 "2024-09-20T00:52:28Z")

</div>

Yarg! This isn’t quite complete. The generated reloctable CMake package from `install(EXPORT` is wrong.

It sets the include path as `<install-prefix>/include` instead of `<install-prefix>/foo/include`

Does anyone have any insights on how to correct this?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 20, 2024, 11:32pm UTC](https://discourse.cmake.org/t/one-cmakelists-txt-file-creating-multiple-relocatable-install-packages/11715/4 "2024-09-20T23:32:44Z")

</div>

Rather than using `PUBLIC_HEADER`, consider using [file sets](https://cmake.org/cmake/help/latest/command/target_sources.html#file-sets) and specify `INCLUDES DESTINATION` instead. Among other things, this has the advantage of preserving directory structure if your headers need to be located in a subdirectory below `foo/include`.

---

<div class="post-metadata">

### Author: ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)
#### Post date: [September 23, 2024, 7:44pm UTC](https://discourse.cmake.org/t/one-cmakelists-txt-file-creating-multiple-relocatable-install-packages/11715/5 "2024-09-23T19:44:05Z")

</div>

Thank you @craig.scott! Using that tip along with some additional google searches I was able to achieve what I wanted.

I created a simple POC for this on github, [cmake-multi-pkg](https://github.com/RochaStratovan/cmake-multi-pkg).

This allowed me to play with the functionality to do what I wanted in the simple case, and now I can translate this into my complex case much easier.

Thank you!
