# How to use \`export\` command together with \`target\_include\_directories\`?

**URL:** <https://discourse.cmake.org/t/how-to-use-export-command-together-with-target-include-directories/6024>\
**Category:** Code\
**Created:** [July 7, 2022, 4:51pm UTC](https://discourse.cmake.org/t/how-to-use-export-command-together-with-target-include-directories/6024 "2022-07-07T16:51:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbahadir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dbahadir/32/197_2.png) [@dbahadir](https://discourse.cmake.org/u/dbahadir)\
**Post date:** [July 7, 2022, 4:51pm UTC](https://discourse.cmake.org/t/how-to-use-export-command-together-with-target-include-directories/6024/1 "2022-07-07T16:51:57Z")

</div>

**tl;dr** : _How am I supposed to export the include search-path containing a target’s public headers when using the `export` command?_

* * *

I thought about trying to use the `export` command for a library target `mylib` in order to not have to `install` the library before using it.

```cmake
export( TARGETS mylib myotherObjectLib FILE mylib-config.cmake )

```

In general, this works, however…

* * *

As libraries very often have some public header files, containing the public API of the library, I thought it would be useful to set the include search-directory (and thereby export it as well):

```cmake
target_include_directories( mylib INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include" )

```

However, this results in CMake complaining:

```auto
CMake Error in CMakeLists.txt:
  Target "mylib" INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/home/user/workspace/source/include"

  which is prefixed in the source directory.

```

* * *

OK… then I maybe try the build-directory instead:

```cmake
target_include_directories( mylib INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/include" )

```

However, this again results in a CMake error:

```auto
CMake Error in CMakeLists.txt:
  Target "mylib" INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/home/user/workspace/build/include"

  which is prefixed in the build directory.

```

* * *

Uhhm… skipping the variable prefix maybe?

```cmake
target_include_directories( mylib INTERFACE "include" )

```

CMake:

```auto
CMake Error in CMakeLists.txt:
  Target "mylib" INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/home/user/workspace/source/include"

  which is prefixed in the source directory.

```

* * *

😕 ❓ … How about I use some other absolute path neither prefixed with the source or build directory?

```cmake
target_include_directories( mylib INTERFACE "/tmp/installed/include" )

```

CMake: “Yes, I like that.”

👀 😵‍💫  
Whaaat? I thought the idea behind `export` was that there is no need for the library to be “installed” outside the build-directory?

On a side-note: As expected, the private build-dependency from `mylib` to the `OBJECT` target `myotherObjectLib` is also exported (which is expected because CMake told me I have to do so), but interestingly its include search path, which is prefixed with the source directory, is exported just fine in the resulting `mylib-config.cmake`.

---

<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:** [October 17, 2022, 9:00pm UTC](https://discourse.cmake.org/t/how-to-use-export-command-together-with-target-include-directories/6024/2 "2022-10-17T21:00:30Z")

</div>

You should wrap build-tree paths in `$<BUILD_INTERFACE>`. The install is complaining that it’s going to generate paths to the source tree and will end up breaking the install tree once it is relocated to another machine.
