# Using PUBLIC FILE\_SET HEADERS with dependent targets

**URL:** https://discourse.cmake.org/t/using-public-file-set-headers-with-dependent-targets/10031
**Category:** Usage
**Created:** [February 8, 2024, 12:13am UTC](https://discourse.cmake.org/t/using-public-file-set-headers-with-dependent-targets/10031 "2024-02-08T00:13:21Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![SherlockInSpace](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sherlockinspace/32/4286_2.png) [@SherlockInSpace](https://discourse.cmake.org/u/SherlockInSpace)
#### Post date: [February 8, 2024, 12:13am UTC](https://discourse.cmake.org/t/using-public-file-set-headers-with-dependent-targets/10031/1 "2024-02-08T00:13:21Z")

</div>

Hey folks, I’m in a bit of bind trying to understand `target_sources(<name> PUBLIC FILE_SET HEADERS ...` and how a target dependent on a library that uses this works.

Here’s my situation, setup, and issue.  
I’ve got a third party library which we’ve introduced CMake to and has a directory structure as follows:

- library/
  - **CMakeLists.txt** - has the following:  
`add_library(...)` and `target_sources(library PUBLIC FILE_SET HEADER BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})`
  - inc/
    - **CMakeLists.txt** (includes a bunch of subdirectories)
    - Framework
      - `Framework.hpp`
      - `Framework.cpp`
      - **CMakeLists.txt** - has the following:  
`target_sources(library PUBLIC FILE_SET HEADERS FILES ${HXX_FILES} PRIVATE ${OUT_CXX_FILES})`  
`target_include_directories(${MAX_LIB_TARGET} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})`

    - Utility/
      - **CMakeLists.txt** - has a similar layout to Framework with source files, but also has subdirectories which follow the same structure
      - Math/
        - **CMakeLists.txt** - …

So, this library builds correctly, and it installs properly into the `/usr/local/...` directories.  
Now, the application that depends on this library uses CPM for a couple reasons, but one of them is that if the version cannot be found locally then CPM is a nice wrapper to go pull the correct one.  
As it stands, if the available version is locally installed, then the application builds and links correctly.

However, if I try to use an upstream version, or local source, I end up with issues where the application cannot find the header files. In fact, the path to the header files is not even on the command line for `gcc` (neither `-I` or `-isystem`).  
Here is what the application looks like

- app/
  - **CMakeLists.txt**  
`add_executable(app app.cpp)`  
`target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})`  
`target_link_libraries(app PRIVATE library)`

That’s it. There’s not a whole lot else.  
Can someone help me determine if what I’m trying to do is possible with CMake. I believe it is, but I’m lost in terms of setting up the **CMakeLists.txt** correctly.

Cheers

---

<div class="post-metadata">

### Author: ![SherlockInSpace](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sherlockinspace/32/4286_2.png) [@SherlockInSpace](https://discourse.cmake.org/u/SherlockInSpace)
#### Post date: [February 13, 2024, 2:04pm UTC](https://discourse.cmake.org/t/using-public-file-set-headers-with-dependent-targets/10031/2 "2024-02-13T14:04:44Z")

</div>

Just bumping to see if anyone has feedback or ideas on this?

---

<div class="post-metadata">

### Author: ![Ryanf55](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ryanf55/32/3516_2.png) [@Ryanf55](https://discourse.cmake.org/u/Ryanf55)
#### Post date: [February 15, 2024, 12:45am UTC](https://discourse.cmake.org/t/using-public-file-set-headers-with-dependent-targets/10031/3 "2024-02-15T00:45:18Z")

</div>

Can you print out the include directory properties of your `library` like so, in `app/CMakeLists.txt`:

```auto
cmake_print_properties(TARGETS library PROPERTIES
                       INTERFACE_INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES)

```

That may give you an idea

---

<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: [February 17, 2024, 5:44am UTC](https://discourse.cmake.org/t/using-public-file-set-headers-with-dependent-targets/10031/4 "2024-02-17T05:44:55Z")

</div>

> [@SherlockInSpace](#):
>
> target\_include\_directories(${MAX\_LIB\_TARGET} PRIVATE ${CMAKE\_CURRENT\_SOURCE\_DIR})

This does not give the right path to consumers. Since you are using `FILE_SET HEADERS`, I think you just want to set `BASE_DIRS` and install the `FILE_SET`. That will set up the include directory usage requirements automatically.

If that doesn’t work, please provide an example that can be executed directly to aid in figuring out what’s up.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [February 17, 2024, 1:00pm UTC](https://discourse.cmake.org/t/using-public-file-set-headers-with-dependent-targets/10031/5 "2024-02-17T13:00:03Z")

</div>

You may try it like this snippet:

```cmake
# ...
# Write spglib_export.h to the current binary directory
include(GenerateExportHeader)
generate_export_header(Spglib_symspg
	EXPORT_FILE_NAME spglib_export.h
	EXPORT_MACRO_NAME API_SPGLIB
)
configure_file(spglib.h spglib.h COPYONLY)

target_sources(Spglib_symspg
	PUBLIC
		FILE_SET HEADERS
		BASE_DIRS ${CMAKE_CURRENT_BINARY_DIR}
		FILES
			${CMAKE_CURRENT_BINARY_DIR}/spglib_export.h
			${CMAKE_CURRENT_BINARY_DIR}/spglib.h
)
target_include_directories(Spglib_symspg
		PRIVATE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
		PUBLIC
		"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
		"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
# ...

```

---

<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: [February 17, 2024, 6:55pm UTC](https://discourse.cmake.org/t/using-public-file-set-headers-with-dependent-targets/10031/6 "2024-02-17T18:55:53Z")

</div>

> [@ClausKlein](#):
>
> ```auto
> target_include_directories(Spglib_symspg
> PRIVATE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
> PUBLIC
> "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
> "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
> )
> 
> ```

This is what `FILE_SET TYPE HEADERS` should do automatically (with a `PRIVATE` fileset for the source tree at least).

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [February 17, 2024, 11:22pm UTC](https://discourse.cmake.org/t/using-public-file-set-headers-with-dependent-targets/10031/7 "2024-02-17T23:22:55Z")

</div>

Yes, it works.
