cmake: check_include_files failing despite setting CMAKE_REQUIRED_INCLUDES

I have the following in CMakeLists.txt:

check_include_files("foo.h" HAVE_FOO)

Despite passing -DCMAKE_REQUIRED_INCLUDES=/directory/containing/fooincludefile to cmake, I still get:

-- Looking for include file foo.h - not found

Is this not the correct usage of check_include_files and CMAKE_REQUIRED_INCLUDES (per the check_include_files documentation)? CMakeError.log shows that the -I/directory/containing/fooincludefile I was hoping for is not being passed to the compiler.

CMAKE_REQUIRED_INCLUDES is usually a local variable. Its value is probably being saved and reset at various places, so your cache value probably doesn’t survive until your check_include_files. I would recommend logging it before the call.