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()?
Thanks!

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.
    Framework
      Only Search Frameworks: 0
      Search Frameworks Last: 0
      Search Frameworks First: 0
    AppBundle
      Only Search AppBundle: 0
      Search AppBundle Last: 0
      Search AppBundle First: 0
    CMAKE_FIND_USE_CMAKE_PATH: 1
    CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
    CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 1
    CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1

  find_file considered the following locations:

    /usr/local/sbin/FakeFile.xyz
    /usr/local/bin/FakeFile.xyz
    /usr/sbin/FakeFile.xyz
    /usr/bin/FakeFile.xyz
    /sbin/FakeFile.xyz
    /bin/FakeFile.xyz
    /usr/games/FakeFile.xyz
    /usr/local/games/FakeFile.xyz
    /usr/local/include/x86_64-linux-gnu/FakeFile.xyz
    /usr/local/include/FakeFile.xyz
    /usr/local/FakeFile.xyz
    /usr/include/x86_64-linux-gnu/FakeFile.xyz
    /usr/include/FakeFile.xyz
    /usr/FakeFile.xyz
    /include/x86_64-linux-gnu/FakeFile.xyz
    /include/FakeFile.xyz
    /opt/cmake-3.17/include/x86_64-linux-gnu/FakeFile.xyz
    /opt/cmake-3.17/include/FakeFile.xyz
    /opt/cmake-3.17/FakeFile.xyz
    /usr/X11R6/include/x86_64-linux-gnu/FakeFile.xyz
    /usr/X11R6/include/FakeFile.xyz
    /usr/X11R6/FakeFile.xyz
    /usr/pkg/include/x86_64-linux-gnu/FakeFile.xyz
    /usr/pkg/include/FakeFile.xyz
    /usr/pkg/FakeFile.xyz
    /opt/include/x86_64-linux-gnu/FakeFile.xyz
    /opt/include/FakeFile.xyz
    /opt/FakeFile.xyz
    /usr/include/X11/FakeFile.xyz

  The item was not found.

1 Like