# Making Generated Header Files Available to Other Targets in the Same Base Project

**URL:** https://discourse.cmake.org/t/making-generated-header-files-available-to-other-targets-in-the-same-base-project/6097
**Category:** Code
**Created:** [July 19, 2022, 3:20pm UTC](https://discourse.cmake.org/t/making-generated-header-files-available-to-other-targets-in-the-same-base-project/6097 "2022-07-19T15:20:15Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![phread](https://discourse.cmake.org/user_avatar/discourse.cmake.org/phread/32/2285_2.png) [@phread](https://discourse.cmake.org/u/phread)
#### Post date: [July 19, 2022, 3:20pm UTC](https://discourse.cmake.org/t/making-generated-header-files-available-to-other-targets-in-the-same-base-project/6097/1 "2022-07-19T15:20:15Z")

</div>

Given some package which generates code including header files (typically associated with a library).  
Is there a recommended way of making generated headers available to other targets?

I have an approach which works but it seems clumsy to me.  
I have the following structure (this example uses ProtoBuf but it could be anything).

base

- CMakeLists.txt
- generated
  - CMakeLists.txt
  - foo.pro

- consume
  - CMakeLists.txt
  - main.cpp

base/CMakeLists.txt

```auto
...
add_subdirectory(generated)
add_subdirectory(consume)
...

```

base/generate/CMakeLists.txt

```auto
...
find_package(Protobuf CONFIG REQUIRED)
set(PROTO_SOURCES foo.proto)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_SOURCES})
get_filename_component(FOO_PROTO_HDR_DIR ${PROTO_HDRS} DIRECTORY)
set(FOO_PROTO_HDRS ${FOO_PROTO_HDR_DIR} PARENT_SCOPE)
...

```

base/consume/CMakeLists.txt

```auto
...
target_include_directories(FooConsumer PRIVATE
        ${CMAKE_CURRENT_BINARY_DIR}
        ${FOO_PROTO_HDRS}
        ${Protobuf_INCLUDE_DIRS}
...

```

Approaches I considered:

- return an include directory from the sub-project using PARENT\_SCOPE (this is demonstrated above)
- write the header files into a well known (base level) directory (where would that be?)

---

<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: [July 21, 2022, 2:02pm UTC](https://discourse.cmake.org/t/making-generated-header-files-available-to-other-targets-in-the-same-base-project/6097/2 "2022-07-21T14:02:39Z")

</div>

> [@phread](#):
>
> return an include directory from the sub-project using PARENT\_SCOPE (this is demonstrated above)

Instead, add the path as a `PUBLIC` usage requirement on the target “providing” the header. Something like:

```cmake
target_include_directories(tgt PUBLIC
  "$<BUILD_INTERFACE:${FOO_PROTO_HDR_DIR}>")

```

The `$<BUILD_INTERFACE>` genex is used since this path won’t be valid for an installation.
