I am having a hard time figuring out how to install PUBLIC
headers specified in target_sources()
.
It appears that target_sources()
is somewhat of a mystery for anything other than adding private sources to an executable.
After reading a lot of material, where especially this blog entry was helpful, I managed to understand & bypass the issue with target_sources()
and PUBLIC files. A CMakeLists.txt
in one of the many subdirectories of my C++ library project looks like this:
target_sources(mylib
PRIVATE
foo.cpp
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/foo.hpp>
$<INSTALL_INTERFACE:foo.hpp>
)
This allows me to successfully build the library. However, upon installation, the header file(s) listed in the PUBLIC
section of target_sources()
are never installed.
My installation looks like this:
install(
TARGETS
mylib
EXPORT mylib-targets
LIBRARY
DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT lib
ARCHIVE
DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT lib
RUNTIME
DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT bin
INCLUDES
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mylib
However, this doesn't install any of the headers.
This stackoverflow answer mentions the use of PUBLIC_HEADER
but reading the documentation doesn't give me the feeling that that is relevant in my case.
Question: What is the proper way of installing PUBLIC
or INTERFACE
headers using install()
?
Background: My goal is to have a separate CMakeLists.txt
in every subdirectory of my source tree. Each of these lists is supposed to use target_sources()
to add PRIVATE
and PUBLIC
files to the build. All PUBLIC
(and INTERFACE
) sources should be installed during installation.