Right way to look for include files in standard system directories?

Pardon my very newbie question - I want cmake to look in “standard” system locations for installed third-party headers - how to do that in a portable way? The variable CMAKE_SYSTEM_INCLUDE_PATH looks promising, e.g. as a “hint” to find_file(), but on my ubuntu 18.03 system CMAKE_SYSTEM_INCLUDE_PATH just contains /usr/include/X11. Do I have to manually include “/usr/local/include” in the hints to find_file()?

Hi Tom, I think CMAKE_INCLUDE_PATH might be the place to put this.
It sounds like your CMake script is using find_file() directly. You could use PATHS or HINTS of find_file() directly in your CMake script, or use CMAKE_INCLUDE_PATH to more generically add include search path for all the find_file().

I run into this kind of issue for example when using Homebrew on MacOS, needing to add the Homebrew location like this.

1 Like

Thanks @scivision. So how/where does CMAKE_INCLUDE_PATH get set to a “reasonable” value for the host where you are building, e.g. on MacOS? Is there some way that cmake can detect that? That is the part I think I am missing…

All the find commands including find_file will search /usr/local on Linux. This is done as /usr/local is contained in CMAKE_SYSTEM_PREFIX_PATH.

For example if we have the following code:

find_file(FakeFileVar FakeFile.xyz)

We can use CMake 3.17+ and to verify the searching with cmake -B <build_dir> --debug-find and looking at the output. As shown below we can see CMake searching /usr/local/include

Running with debug output on for the `find` commands.
CMake Debug Log at CMakeLists.txt:7 (find_file):
  find_file called with the following settings:

    VAR: FakeFileVar
    NAMES: "FakeFile.xyz"
    Documentation: Path to a file.
      Only Search Frameworks: 0
      Search Frameworks Last: 0
      Search Frameworks First: 0
      Only Search AppBundle: 0
      Search AppBundle Last: 0
      Search AppBundle First: 0

  find_file considered the following locations:


  The item was not found.

1 Like