It's unclear what to do with target_sources() in INTERFACE libraries
So the Internet is still divided on whether header files go into target_sources() or not (and AFAIK the cmake docs are silent on it). But IDEs keep depending on it and https://github.com/Bagira80/More-Modern-CMake/raw/master/OhNoMoreModernCMake.pdf has just said they should go there.
Which brings us to header-only/INTERFACE libraries. I would want to do
cmake_minimum_required(VERSION 3.14)
project(test)
include(GNUInstallDirs)
add_library(test INTERFACE)
target_sources(test PRIVATE include/la.h)
target_include_directories(test INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
install(DIRECTORY include DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS test EXPORT test-config)
install(EXPORT test-config NAMESPACE test:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/test)
But it doesn't like my target_sources
:
CMake Error at CMakeLists.txt:6 (target_sources):
target_sources may only set INTERFACE properties on INTERFACE targets
So let's change it for target_sources(test INTERFACE include/la.h)
then which... is weird. Should that file really be in INTERFACE_SOURCES
? The whole headers handling in cmake is a bit strange, with their installation being done with install(DIRECTORY)
, with no real "these are headers" semantic in there. But whatever, let's do it...
CMake Error in CMakeLists.txt:
Target "test" INTERFACE_SOURCES property contains path:
"<MY_SOURCE_DIR>/include/la.h"
which is prefixed in the source directory.
Not sure how <MY_SOURCE_DIR>
(my actual source directory is in there, I replaced it here) got in there. I didn't include it in my target_sources()
, used a relative path. But I'm stuck here, AFAIK I simply can't include the headers in there and my IDE will not help me.
I'm using cmake 3.14.5, but didn't see anything about this in 3.15/3.16 release notes.