CMake -如何在安装(目标)中安装(目录)?

ftf50wuq  于 2023-03-08  发布在  其他
关注(0)|答案(2)|浏览(240)

这段代码的目的是将包含include文件夹的静态库复制到另一个目录中。
新目录将具有以下结构

output_dir
    ├───include
    ├───lib
         └───win64

我有两行install的CMake代码,我想把它们减少到一行。首先,我把lib目标安装到output_dir/lib/win64,把include目录安装到output_dir/include

add_library(lib STATIC test.cpp )

target_include_directories(lib 
    PUBLIC 
        ${PROJECT_SOURCE_DIR}/include
)

install(TARGETS lib
    ARCHIVE
        DESTINATION 
            ${PROJECT_SOURCE_DIR}/build/output_dir/lib/win64
)

install(DIRECTORY
            ${PROJECT_SOURCE_DIR}/include
        DESTINATION
            ${PROJECT_SOURCE_DIR}/build/output_dir/include
)

我想有效地将两个安装命令减少到一个,下面是我最好的尝试。静态lib库被正确地复制,但是目录被忽略。

install(TARGETS lib
        ARCHIVE
            DESTINATION 
                ${PROJECT_SOURCE_DIR}/build/output_dir/lib/win64
        PUBLIC_HEADER
            DESTINATION
                ${PROJECT_SOURCE_DIR}/build/output_dir/
        INCLUDES
            DESTINATION
                ${PROJECT_SOURCE_DIR}/build/output_dir/     
)

我知道public_header有错误,但是我做错了什么?

afdcj2ne

afdcj2ne1#

install()命令中只需要PUBLIC_HEADER参数,而不需要INCLUDES参数。但是,PUBLIC_HEADER参数不会获取任何头文件,因为尚未设置lib目标的PUBLIC_HEADER属性。请尝试以下操作:

add_library(lib STATIC test.cpp)

target_include_directories(lib 
    PUBLIC 
        ${PROJECT_SOURCE_DIR}/include
)

# List the headers we want to declare as public for installation.
set(MY_PUBLIC_HEADERS
    ${PROJECT_SOURCE_DIR}/include/MyHeader.hpp
    ${PROJECT_SOURCE_DIR}/include/MyHeader2.hpp
    ...
)

# Tell the target these headers are "public".
set_target_properties(lib PROPERTIES PUBLIC_HEADER "${MY_PUBLIC_HEADERS}")

install(TARGETS lib
    ARCHIVE
        DESTINATION ${PROJECT_SOURCE_DIR}/build/output_dir/lib/win64
    PUBLIC_HEADER
        DESTINATION ${PROJECT_SOURCE_DIR}/build/output_dir   
)
gijlo24d

gijlo24d2#

如果你们中有人感兴趣的话,下面是我添加spdlog include目录(包括嵌套的子目录)的代码,它确实使用了GLOB_RECURSE,但是我找不到更好的方法。
注意:需要CMake〉= 3.23(对于FILE_SET)。

set(Target logger)
add_library(${Target} SHARED logger.cpp)

# finding recursively all the header files
file(GLOB_RECURSE spdlog_headers
    ${CMAKE_CURRENT_SOURCE_DIR}/logger_interface/spdlog/include/*.h)
target_sources(${Target} PUBLIC FILE_SET HEADERS 
    BASE_DIRS logger_interface/spdlog/include
    FILES ${spdlog_headers})
set_target_properties(${Target} PROPERTIES FILE_SET spdlog_headers)
set_target_properties(${Target} PROPERTIES PUBLIC_HEADER "logger.h") # my main include file

set(InstallDir ${Target}) # with skbuild
set(InstallDir ${CMAKE_BINARY_DIR}/install/${Target}) # with cmake normal compilation
install(
    TARGETS logger
    FILE_SET HEADERS DESTINATION ${InstallDir}/include
    PUBLIC_HEADER DESTINATION ${InstallDir}/include
    RUNTIME DESTINATION ${InstallDir}/bin
    LIBRARY DESTINATION ${InstallDir}/lib
    ARCHIVE DESTINATION ${InstallDir}/lib)

相关问题