How to configure CMakeLists.txt to install public headers of a shared library?

后端 未结 2 840
天涯浪人
天涯浪人 2020-12-18 00:48

I want to use cmake to install my library edv but when I execute:

cmake --build . --target install

It installs but it only cre

2条回答
  •  旧时难觅i
    2020-12-18 01:33

    To install all headers present in the ./include folder, all you need to do is:

    • set a list with all the header files you want to install (i.e. define EDV_INCLUDE_FILES),

    • set the target property PUBLIC_HEADER with all those header files,

    • set the PUBLIC_HEADER argument in install(TARGETS ...) with the installation directory.

    This method is also the basis for CMake's support of macOS frameworks.

    I've updated the example above with the settings to install the project's public headers in ./include in the project's target output directory. Caveat: I haven't personally tested the project definition, thus it may require some minor tweaks to work.

    cmake_minimum_required(VERSION 3.12)
    
    project(edv)
    
    # include PUBLIC directories
    set(EDV_PUBLIC_INCLUDE_DIRECTORIES      include/ )
    
    set(EDV_PRIVATE_INCLUDE_DIRECTORIES     src/   )
    
    # Edv source files list
    file(GLOB_RECURSE EDV_SOURCE_FILES "src/*.cpp" "src/*.hpp*")
    
    file(GLOB_RECURSE EDV_INCLUDE_FILES "include/*.hpp*")
    
    # build the library
    add_library(${PROJECT_NAME} SHARED ${EDV_INCLUDE_FILES} ${EDV_SOURCE_FILES} )
    
    target_include_directories(${PROJECT_NAME} PUBLIC ${EDV_PUBLIC_INCLUDE_DIRECTORIES})
    target_include_directories(${PROJECT_NAME} PRIVATE ${EDV_PRIVATE_INCLUDE_DIRECTORIES})
    
    set_target_properties(
        PUBLIC_HEADER "${EDV_INCLUDE_FILES}"
    )
    
    install (TARGETS ${PROJECT_NAME}
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        PUBLIC_HEADER DESTINATION include
    )
    

提交回复
热议问题