From CMake 3.23 version target_source
contain FILE_SET option. Now you can define your headers in one place and automatically use them on an install operation. Even include_directories
it’s not any more necessary. This gives more improvements and can be used for modules export, for details read the documentation, install section also.
The above example can be written like this:
target_sources(
mylib
PUBLIC
FILE_SET public-headers
TYPE HEADERS
FILES
myfirstpublicheader.h
mysecondpublicheader.h
)
install(TARGETS mylib FILE_SET public-headers)