0

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.

Samuel
  • 816
  • 1
  • 7
  • 13
  • Possible duplicate of [CMAKE - How to properly copy static library's header file into /usr/include?](https://stackoverflow.com/questions/10487256/cmake-how-to-properly-copy-static-librarys-header-file-into-usr-include) – rustyx Feb 07 '19 at 07:50
  • How can you have a `.a` when you don't have a `.cpp`? If you set `target_include_directories` with `PUBLIC`, then the include paths will be added to the dependent projects. That's what you want, if you actually had a static library (which you don't). – Matthieu Brucher Feb 07 '19 at 07:58
  • I do have a .cpp, the class has a .cpp. The .a has to come from the class which must include the interface because it is the parent. – Samuel Feb 07 '19 at 10:49

0 Answers0