How to use CHECK_INCLUDE_FILES macro in cmake?

I can't say I'm a huge fan of CheckIncludeFiles because of its difficulty to get right. In principal it's good - it actually creates tiny c files which #include the requested headers and tries to compile them, but it seems to be too easy to get wrong.

I generally prefer just using find_path and/or find_file for this job. This doesn't check the contents of any files found, but usually if you find the required header, its contents are good!

I would use find_path if I needed to know the folder where the header lived. This would usually be because I need to check for other files in the same folder (as in your case), or more commonly because I need to add the folder to an include_directories call.

find_file yields the full path to the file (if found). For headers, normally I don't need the path elsewhere in the CMakeLists - it's just used immediately after the find_file to check the file was actually found.

So, here's how I'd go about checking for "gssapi/gssapi.h" and "gssapi/gssapi_krb5.h"

find_path(GssApiIncludes gssapi.h PATHS <list of folders you'd expect to find it in>)
if(NOT GssApiIncludes)
  message(FATAL_ERROR "Can't find folder containing gssapi.h")
endif()

find_file(GssKrb gssapi_krb5.h PATHS ${GssApiIncludes} NO_DEFAULT_PATH)
if(NOT GssKrb)
  message(FATAL_ERROR "Can't find gssapi_krb5.h in ${GssApiIncludes}")
endif()

If you do this, then if required you could add

include_directories(${GssApiIncludes})

so that in your source code you can do

#include "gssapi.h"
#include "gssapi_krb5.h"

Tags:

Cmake