Home > Software design >  target_include_directories not including INTERFACE library
target_include_directories not including INTERFACE library

Time:08-13

# Adding a header-only library
set(INCLUDE_LOCATION "${PROJECT_SOURCE_DIR}/../include")
add_library(myLib INTERFACE)
target_include_directories(myLib INTERFACE "${INCLUDE_LOCATION}")

# Printing the include paths for the header only target
get_target_property(dirs myLib INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
    message(STATUS "dir='${dir}'")
endforeach()

The above cmake will show dir=NOTFOUND

There are no problems with ${INCLUDE_LOCATION}, I have checked it.

This works perfectly fine if I swap the library to executable.

I am not sure what I am missing.

CodePudding user response:

Interface libraries don't have non-INTERFACE properties. INCLUDE_DIRECTORIES is simply invalid here.

You are looking for INTERFACE_INCLUDE_DIRECTORIES.

cmake_minimum_required(VERSION 3.24)
project(test)

set(INCLUDE_LOCATION "${PROJECT_SOURCE_DIR}/../include")
add_library(myLib INTERFACE)
target_include_directories(myLib INTERFACE "${INCLUDE_LOCATION}")

get_target_property(dirs myLib INTERFACE_INCLUDE_DIRECTORIES)
foreach(dir IN LISTS dirs)
    message(STATUS "dir='${dir}'")
endforeach()

Prints:

-- dir='/home/reinking/test/../include'
  • Related