Issue
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?
Solution
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
)
Answered By - Kevin