CHECK_INCLUDE_FILE_CXX does not respect 'target_include_directories' when looking for include file

Viewed 96

I'm trying to build simple-web-server using a local standalone copy of asios. As I don't have the library installed, and I can't install it due to security restrictions, I've modified the cmakelists file just a bit, to tell it where to search for the include file. I can clearly see that it's finding the location, but CHECK_INCLUDE_FILE_CXX isn't finding it for some reason, even though I've added the directory with target_include_directories. What is the correct way to do this?

if(USE_STANDALONE_ASIO)

### BEGIN CUSTOM ADDITIONS ###

    message(NOTICE "Searching for asio.hpp")
    find_path(ASIO_PATH asio.hpp ./asio/include) 
    if(ASIO_PATH-NOTFOUND)
        message(AUTHOR_WARNING "Asio not found in ./asio/include")
    else()
        message(NOTICE "asio.hpp found in ${ASIO_PATH}")
        target_include_directories(simple-web-server INTERFACE ASIO_PATH)
    endif()

### END CUSTOM ADDITIONS ###    

    find_package(Threads REQUIRED)
    target_link_libraries(simple-web-server INTERFACE ${CMAKE_THREAD_LIBS_INIT})
    
   target_include_directories
        target_compile_definitions(simple-web-server INTERFACE USE_STANDALONE_ASIO)
        include(CheckIncludeFileCXX)
        CHECK_INCLUDE_FILE_CXX(asio.hpp HAVE_ASIO)
        if(NOT HAVE_ASIO)
            message(FATAL_ERROR "Standalone Asio not found")
        endif()
1 Answers

Command target_include_directories and include_directories affects on compilation, but doesn't affect on checking headers via CHECK_INCLUDE_FILE_CXX.

For hint the macro CHECK_INCLUDE_FILE_CXX to search in additional include directories, set variable CMAKE_REQUIRED_INCLUDES:

list(APPEND CMAKE_REQUIRED_INCLUDES ${ASIO_PATH})
...
CHECK_INCLUDE_FILE_CXX(asio.hpp HAVE_ASIO)

This variable is described in the documentation:

CMAKE_REQUIRED_INCLUDES

a list of header search paths to pass to the compiler.


While internally CHECK_INCLUDE_FILE_CXX performs compilation, it compiles in other CMake project (via try_compile). That other project doesn't receive properties (like INCLUDE_DIRECTORIES) of the main project. Instead, a specific set of variables is explicitly passed from the main project, and CMAKE_REQUIRED_INCLUDES is among that variables.

Related