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

时间:2019-09-05 01:50:52

标签: visual-studio cmake

我的这段代码的目的是将包含includes文件夹的静态lib复制到另一个目录中。

新目录将具有结构

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有关,但是我在做什么错呢?

1 个答案:

答案 0 :(得分:0)

您应该只需要PUBLIC_HEADER命令中的install()参数,而不是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   
)