I am creating an INTERFACE IMPORTED header-only library whose name, Vulkan::Headers, I add in a target_include_directories command but target_include_directories is treating it as a relative path name. Here are the details. I have two CMake files (edited to the essentials for brevity).
vulkanhaders.cmake
if(DEFINED ENV{VULKAN_SDK})
find_path(DFDUtils_Vulkan_INCLUDE_DIR
NAMES
vulkan/vulkan.h
HINTS
$ENV{VULKAN_SDK}/include
REQUIRED
)
else()
message(FATAL_ERROR "VULKAN_SDK not defined")
endif()
if(NOT TARGET Vulkan::Headers)
add_library(Vulkan::Headers INTERFACE IMPORTED)
set_target_properties(Vulkan::Headers
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${DFDUtils_Vulkan_INCLUDE_DIR}"
)
endif()
CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(dfdutils C)
include(GNUInstallDirs) # Must be after project.
include(CMakePrintHelpers)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
include(vulkanheaders.cmake)
set(DFDUTILS_LINK_TYPE STATIC)
if(DFDUTILS_EMBED)
set(DFDUTILS_LINK_TYPE OBJECT)
endif()
#
# --- DFD Utils library ---
#
add_library(dfdutils ${DFDUTILS_LINK_TYPE}
createdfd.c
colourspaces.c
dfd.h
interpretdfd.c
KHR/khr_df.h
printdfd.c
queries.c
vk2dfd.c
dfd2vk.c
)
get_target_property(vulkan_headers Vulkan::Headers INTERFACE_INCLUDE_DIRECTORIES)
cmake_print_variables(vulkan_headers DFDUtils_Vulkan_INCLUDE_DIR)
target_include_directories(dfdutils
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
INTERFACE
Vulkan::Headers)
get_target_property(include_dirs dfdutils INCLUDE_DIRECTORIES)
cmake_print_variables(include_dirs)
get_target_property(interface_include_dirs dfdutils INTERFACE_INCLUDE_DIRECTORIES)
cmake_print_variables(interface_include_dirs)
# Remainder of file removed.
Here is the output of config showing the values of the variables being printed
vulkan_headers="/Users/mark/VulkanSDK/1.4.357.0/macOS/include" ; DFDUtils_Vulkan_INCLUDE_DIR="/Users/mark/VulkanSDK/1.4.357.0/macOS/include"
include_dirs="/Users/mark/Projects/khronos/github/dfdutils;/Users/mark/Projects/khronos/github/dfdutils/Vulkan::Headers"
interface_include_dirs="/Users/mark/Projects/khronos/github/dfdutils;/Users/mark/Projects/khronos/github/dfdutils/Vulkan::Headers"
Configuring done (0.1s)
As you can see, target_include_directories doesn’t recognize Vulkan::Headers as a library, thinks it is a relative path and prepends ${CMAKE_CURRENT_SOURCE_DIR} to it.
I can’t see what I am doing wrong, probably something stupid so I am looking for help. The documentation for target_include_directories does not show this usage but I have seen it in practice many times and use it myself in other projects such as when adding SDL3::Headers, which is created by a config file for finding SDL3. As far as I can see the way I am creating the INTERFACE IMPORT library is the same as used in various config and find modules that I have looked at.