Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - How to install(DIRECTORY) in install(TARGETS)?

My objective of this code is to copy over a static lib with a folder of includes into another directory.

The new directory will have the structure

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

I have two install lines of CMake code that I want to reduce to one. First, I install the lib target to output_dir/lib/win64 and the include directory to 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
)

I'd like to effectively reduce the two install commands to one, here is my best attempt at doing so. The static lib library gets copied over correctly but the directories get ignored.

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/     
)

I know the error is with public_header, but what am I doing wrong?

like image 365
j__gt Avatar asked Jan 27 '26 01:01

j__gt


1 Answers

You should only need the PUBLIC_HEADER argument in the install() command, not the INCLUDES argument. However, your PUBLIC_HEADER argument will not grab any header files, because the PUBLIC_HEADER property for the lib target has not been set. Try something like this:

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   
)
like image 105
Kevin Avatar answered Jan 31 '26 04:01

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!