# How to portably specify header search paths to target\_include\_directories()?

**URL:** https://discourse.cmake.org/t/how-to-portably-specify-header-search-paths-to-target-include-directories/2638
**Category:** Code
**Created:** [January 27, 2021, 1:37am UTC](https://discourse.cmake.org/t/how-to-portably-specify-header-search-paths-to-target-include-directories/2638 "2021-01-27T01:37:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tomoreilly](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomoreilly](https://discourse.cmake.org/u/tomoreilly)
#### Post date: [January 27, 2021, 1:37am UTC](https://discourse.cmake.org/t/how-to-portably-specify-header-search-paths-to-target-include-directories/2638/1 "2021-01-27T01:37:02Z")

</div>

Library mylib contains file foo.c which includes:

`#include <Xm/Xm.h>`

On my linux system, file /usr/include/Xm/Xm.h exists and /usr/include is treated as a “system” header directory by the compiler, and so there’s no need to specify the header search path to the compiler. But on my Mac, the file is in /usr/local/include/Xm/Xm.h and /usr/local/include is NOT considered a system header directory, and so the header path must explicitly be provided to the compiler. How should target\_include\_directories() be used in this case, so that foo.c compiles properly on both my linux and MacOS machines (and other architectures)? Something like:

```
add_library(mylib STATIC foo.c)
 
target_include_directories(mylib PUBLIC some-search-path…)

```

What should some-search-path look like? Is there an existing CMAKE variable I should use here?  
Thanks!

---

<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: [January 27, 2021, 3:30pm UTC](https://discourse.cmake.org/t/how-to-portably-specify-header-search-paths-to-target-include-directories/2638/2 "2021-01-27T15:30:32Z")

</div>

You want something like:

```cmake
find_file(Xm_INCLUDE_DIR
  NAMES Xm/Xm.h)
mark_as_advanced(Xm_INCLUDE_DIR)
if (NOT Xm_INCLUDE_DIR)
  message(FATAL_ERROR # or whatever is appropriate
    "Xm.h not found")
endif ()

target_include_directories(mylib PUBLIC ${Xm_INCLUDE_DIR})

```

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [January 27, 2021, 3:59pm UTC](https://discourse.cmake.org/t/how-to-portably-specify-header-search-paths-to-target-include-directories/2638/3 "2021-01-27T15:59:40Z")

</div>

IIRC `<Xm/Xm.h>` are the Motif widgets. You may want to have a look onto FindMotif (.cmake) in the documentation or CMake modules directory. Then you can easily use `find_package(Motif REQUIRED)` in your CMakeLists.txt file.

---

<div class="post-metadata">

### Author: ![tomoreilly](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomoreilly](https://discourse.cmake.org/u/tomoreilly)
#### Post date: [January 27, 2021, 5:31pm UTC](https://discourse.cmake.org/t/how-to-portably-specify-header-search-paths-to-target-include-directories/2638/4 "2021-01-27T17:31:18Z")

</div>

Thanks! How is FindMotif used, what arguments, etc? I am unable to find any usage examples. [The documentation is very brief.](https://cmake.org/cmake/help/latest/module/FindMotif.html)

---

<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: [January 27, 2021, 5:56pm UTC](https://discourse.cmake.org/t/how-to-portably-specify-header-search-paths-to-target-include-directories/2638/5 "2021-01-27T17:56:15Z")

</div>

You should use it via `find_package(Motif)`. See the [`find_package` docs](https://cmake.org/cmake/help/latest/command/find_package.html) for how to use it. The FindMotif module itself documents variables and imported targets it creates which can be used after using `find_package` on it.
