I have an interface in c++ which is parent of a class. What I want is to make a project with cmake with 2 different CMakelists, one for the interface, and the other one for the class in a subdirectory. Then, I need to generate a static library from the class. That means, generating the .a file and installing the targets with the needed header files that .a requires. I'm having trouble because I only get to install the .h from the class. The interface headers, which are included in the class headers, aren't installed and not found. What's the correct way to include the interface in the class and install and export the targets properly?
That's the structure:
|-------> interface
| |----> CMakeLists.txt
| |----> include
| |----> interface.hpp
| |----> class
| |----> CMakeLists.txt
| |----> include
| |----> class.hpp
| |----> src
| |----> class.cpp
The code is here.
Interface
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/class ${CMAKE_CURRENT_BINARY_DIR}/class)
add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
install(TARGETS ${PROJECT_NAME} EXPORT ${PRODUCT_TARGET})
Class
add_library(${PROJECT_NAME} src/class.cpp include/class.hpp)
set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "include/class.hpp")
target_include_directories(${PROJECT_NAME}
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE $<TARGET_PROPERTY:interface,INTERFACE_INCLUDE_DIRECTORIES>)
install(TARGETS ${PROJECT_NAME} EXPORT ${PRODUCT_TARGET}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
PUBLIC_HEADER DESTINATION include/${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME} INTERFACE interface)
EDIT 1 *** The generation of the .a is OK but when I'm using it in another project and I include the class.h it does not found the interfaces headers.