0

I'm trying to restructure my CMAKE project by placing all headers in a separate cmake header only project and add that using target_link_libraries to my sources cmake project.

Is it possible to retain folder path while including headers in cpp source file.

Headers in interface library should also be able to access other headers from different directory(within same cmake interface project) using header folder name.

expected

#include "A/A.hpp"
#include "AGen/AGen.hpp"
#include "json/json.hpp"

current working impl

#include "A.hpp"
#include "AGen.hpp"
#include "json.hpp"

my interface cmake configuration

    set(project_includes_internal
    ${CMAKE_CURRENT_LIST_DIR}/A
    ${CMAKE_CURRENT_LIST_DIR}/B
    ${CMAKE_CURRENT_LIST_DIR}/C
    )
    
    set(project_gen_includes
    ${CMAKE_CURRENT_BINARY_DIR}/include/AGen
    ${CMAKE_CURRENT_BINARY_DIR}/include/BGen
    ${CMAKE_CURRENT_BINARY_DIR}/include/CGen
    )
    
    set(project_dep_inludes
    ${CMAKE_HOME_DIRECTORY}/dep/nlohmann/json
    )
set(PROJECT_INCLUDES
        ${project_includes_internal}
        ${project_gen_includes}
        ${project_dep_inludes})

add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME}
        INTERFACE
        $<BUILD_INTERFACE:${PROJECT_INCLUDES}>
        $<INSTALL_INTERFACE:include>
        )

my sources cmake config

add_executable(${PROJECT_NAME}-bin ${PROJECT_TARGET_SOURCES})
target_link_libraries(${PROJECT_NAME}-bin
        PUBLIC
        ${ACTIVATION_PROJECT_INCLUDES_NAME})

Also is it possible to copy all required include files in a build directory so those can be copied/tar published while installing on other machines.

0x52616A657368
  • 388
  • 3
  • 24
  • I don't understand what's the problem here. If you want to do `#include "AGen/AGen.hpp"` then add `include` directory to `target_include_directories` instead of `include/AGen`. Also, "internal" suggests that these headers should be visible by implementation only, so why add them to the interface? – pptaszni Oct 19 '21 at 15:47
  • @pptaszni not sure what exactly you mean by "internal suggests that these headers should be visible by implementation only, so why add them to the interface?" basically i'm trying to create a header only lib and add that as target instead of having headers listed in source cmake file – 0x52616A657368 Oct 19 '21 at 16:26
  • Why do you call the first collection of headers `project_includes_internal`? I though because they are internal impl details and I wanted to advise against including them in the interface. Otherwise, everything you show is correct. Do you experience problems including the headers in the code? Your (a bit simplified) example works for me. – pptaszni Oct 19 '21 at 16:31

0 Answers0