Feature request: option for target_link_libraries that make libraries PRIVATE and includes/definitions PUBLIC
I am building a library that is linked to a static library. I am trying to figure out a way to propagate just the include/definitions and not the library when I am exporting the targets for the library I am building. Below is an example CMakeLists of the static library
cmake_minimum_required(VERSION 3.7)
project(mystaticlib)
set(PROJECT_SRCS ${PROJECT_SOURCE_DIR}/src/mystatic.cpp)
set(PROJECT_INCS ${PROJECT_SOURCE_DIR}/include/mystatic.h)
add_library(${PROJECT_NAME} STATIC ${PROJECT_SRCS} ${PROJECT_INCS})
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
export(TARGETS ${PROJECT_NAME}
APPEND FILE ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}Targets.cmake)
very standard and nothing special, now here is my CMakeLists of the library that is linking to this static library
cmake_minimum_required(VERSION 3.7)
project(testlib)
set(PROJECT_SRCS ${PROJECT_SOURCE_DIR}/src/mytest.cpp)
set(PROJECT_INCS ${PROJECT_SOURCE_DIR}/include/mytest.h)
add_library(${PROJECT_NAME} SHARED ${PROJECT_SRCS} ${PROJECT_INCS})
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
target_link_libraries(${PROJECT_NAME} PRIVATE mystaticlib)
# target_link_libraries(${PROJECT_NAME} PUBLIC mystaticlib)
# target_link_libraries(${PROJECT_NAME} INTERFACE mystaticlib)
export(TARGETS ${PROJECT_NAME}
APPEND FILE ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}Targets.cmake)
The problem of target_link_libraries
with PRIVATE
is the include directories from the static library does not get exported. Hence external users links to testlib
it won't be able to locate where the headers of the mystaticlib
are.
The problem of target_link_libraries
with PUBLIC
is exposing the static library to the external users which is not desired. I only want the external user to be able to link to testlib
.
target_link_libraries
with INTERFACE
does not even compile.
Can we get an option that make the library PRIVATE
and includes/definitions PUBLIC
?