I’m writing two C++ project, the upstream one providing some header-only libraries. I wonder what is the right (i.e. modern-CMake) way to “export” the headers folder from upstream and use it in downstream. I know I can export some custom variables but I’d like to stick with a streamlined solution if it exists.
If those are two different projects, then I’d make a proper installable CMake package for the upstream project, and then add it to the downstream project with the usual:
find_package(UpstreamHeaders CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME}
PRIVATE
upstream::headers # or UpstreamHeaders, or whichever you choose
)
What I don’t understand is how to define the upstream::headers target in the upstream project. I guess that in your example it is an empty library which is used just to include the upstream header folder in downstream targets linking it, right? IF yes, then I cannot create something similar in my upstream project since defining no source file I get this error:
CMake Error at src/common/CMakeLists.txt:7 (add_library):
No SOURCES given to target: KM3NeTCommon
Or did I misunderstood your suggestion?
Got it:
add_library(EmptyLib INTERFACE)
target_include_directories(EmptyLib INTERFACE $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>)
Thanks for your help!
I didn’t get what you mean either.
A header-only library is declared with:
add_library(UpstreamHeaders INTERFACE)
and it does not need to have sources. It will need to have target_include_directories() with INTERFACE scope though (and the corresponding generator expressions for BUILD_INTERFACE and INSTALL_INTERFACE), but that you seem to have figured out.
To make it “namespaced”, one usually does the following:
add_library(upstream::headers ALIAS UpstreamHeaders)
and also(?) provide NAMESPACE argument for the install() command that exports target(s).
Sorry for not being clear, my question was basically how to define a header-only library, thanks again for your help.