How to portably specify header search paths to target_include_directories()?

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!

You want something like:

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})
1 Like

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.

1 Like

Thanks! How is FindMotif used, what arguments, etc? I am unable to find any usage examples. The documentation is very brief.

You should use it via find_package(Motif). See the find_package docs 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.

1 Like