# How to install an interface library with generated source in build folder

**URL:** https://discourse.cmake.org/t/how-to-install-an-interface-library-with-generated-source-in-build-folder/5449
**Category:** Usage
**Tags:** os:linux, os:windows
**Created:** [April 12, 2022, 3:16pm UTC](https://discourse.cmake.org/t/how-to-install-an-interface-library-with-generated-source-in-build-folder/5449 "2022-04-12T15:16:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![gabyx](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gabyx/32/2107_2.png) [@gabyx](https://discourse.cmake.org/u/gabyx)
#### Post date: [April 12, 2022, 3:16pm UTC](https://discourse.cmake.org/t/how-to-install-an-interface-library-with-generated-source-in-build-folder/5449/1 "2022-04-12T15:16:44Z")

</div>

We have a header-only library `apiDefines` (`INTERFACE`) which has a generated `API.h` (declspec imports/exports) as source which is used as a helper target to link to in other library targets:

```cmake
  set(generatedFile ${CMAKE_CURRENT_BINARY_DIR}/API.h) 
  set_property(SOURCE ${generatedFile} PROPERTY GENERATED ON)

  set(linkTarget "apiDefines")
  add_library(${linkTarget} INTERFACE)
  target_sources(${linkTarget} PUBLIC ${generatedFile})
  set_property(TARGET ${linkTarget} PROPERTY PUBLIC_HEADER)

  target_compile_definitions(${linkTarget}
                             INTERFACE "API_EXPORTS")
  target_include_directories(${linkTarget}
                             INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
                                                  $<INSTALL_INTERFACE:include>)

```

When we want to install this cmake complains about:

```auto
Target "apiDefines" INTERFACE_SOURCES property contains
  path:

    "/workspaces/cmake-general/tests/project/build/linux-clang-libstdc++-debug/src/plugins/internal/API.h" 
 which is prefixed in the build directory.

```

If we use `target_sources(${linkTarget} PRIVATE ${generatedFile})` that would certainly solve the problem, but this is somehow wrong as `API.h` is a public source for `apiDefines`.

I am not quite sure how to achieve that I can install a header-only library which uses generated sources in the binary tree?

---

<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: [April 12, 2022, 4:15pm UTC](https://discourse.cmake.org/t/how-to-install-an-interface-library-with-generated-source-in-build-folder/5449/2 "2022-04-12T16:15:48Z")

</div>

> [@gabyx](#):
>
> `target_sources(${linkTarget} PUBLIC ${generatedFile})`

To do this, you should use CMake 3.23’s [`FILE_SET`](https://cmake.org/cmake/help/latest/command/target_sources.html#file-sets) feature. If you need older versions, just make it `PRIVATE` and make a manual `install(FILES)` call for the headers.

---

<div class="post-metadata">

### Author: ![gabyx](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gabyx/32/2107_2.png) [@gabyx](https://discourse.cmake.org/u/gabyx)
#### Post date: [April 12, 2022, 4:19pm UTC](https://discourse.cmake.org/t/how-to-install-an-interface-library-with-generated-source-in-build-folder/5449/3 "2022-04-12T16:19:26Z")

</div>

Jeah, I exactly did this, and it works flawlessly. Awesome.

```cmake
target_sources(
    ${linkTarget}
    PUBLIC FILE_SET
           "publicHeadersBin"
           TYPE
           HEADERS
           FILES
           ${generatedFile}
           BASE_DIRS
           ${CMAKE_CURRENT_BINARY_DIR})

```

---

<div class="post-metadata">

### Author: ![gabyx](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gabyx/32/2107_2.png) [@gabyx](https://discourse.cmake.org/u/gabyx)
#### Post date: [April 13, 2022, 12:16pm UTC](https://discourse.cmake.org/t/how-to-install-an-interface-library-with-generated-source-in-build-folder/5449/4 "2022-04-13T12:16:13Z")

</div>

@ben.boeckel : The strange thing is when I use FILE\_SET to add the source in the build directory the `add_custom_command` producing the `${generatedFile}` does not run anymore? Is that a bug?

```cmake
 add_custom_command(
    OUTPUT ${generatedFile}
    COMMAND ${cmd}
    MAIN_DEPENDENCY ${configureTemplate}
    COMMENT "Generating API header at '${generatedFile}'."
    VERBATIM)

```

---

<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: [April 13, 2022, 8:49pm UTC](https://discourse.cmake.org/t/how-to-install-an-interface-library-with-generated-source-in-build-folder/5449/5 "2022-04-13T20:49:02Z")

</div>

Yes. I see you filed [this issue](https://gitlab.kitware.com/cmake/cmake/-/issues/23422).
