target_include_directories does not recognize relative path
I am on Ubuntu 16.04, latest cmake. I am following a CMake good practice guideline to replace include_directories
with target_include_directories
. I ran into a problem where target_include_directories
does not seems to recognize relative path. Below is my CMakeLists
project(SampleExample)
set(PROJECT_SRCS
${PROJECT_SOURCE_DIR}/src/SampleExample.cpp
)
add_executable(${PROJECT_NAME} ${PROJECT_SRCS})
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:../../modules/Sample/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
target_link_libraries(${PROJECT_NAME} Sample)
Sample
is a library built within the same repository, I get a CMake error CMake Error in apps/SampleExample/CMakeLists.txt: Found relative path while evaluating include directories of
. If I change it to the below
project(SampleExample)
set(PROJECT_SRCS
${PROJECT_SOURCE_DIR}/src/SampleExample.cpp
)
add_executable(${PROJECT_NAME} ${PROJECT_SRCS})
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/modules/Sample/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
target_link_libraries(${PROJECT_NAME} Sample)
it works, but the downside is if this repository is used as a submodule it will break, since ${CMAKE_SOURCE_DIR}
becomes the root directory of repository who uses this repository as a submodule. And obviously the old ways like below works
project(SampleExample)
set(PROJECT_SRCS
${PROJECT_SOURCE_DIR}/src/SampleExample.cpp
)
include_directories(../../modules/Sample/include)
add_executable(${PROJECT_NAME} ${PROJECT_SRCS})
target_link_libraries(${PROJECT_NAME} Sample)
Is it a bug that target_include_directories can't recognize relative path?