How to group multiple library targets into one in CMake

Viewed 2236

I am trying to group multiple targets into a single one so the downstream user only need to link to that single one. The downstream user won't need to look up all the targets and all functionality from the upstream library will be available by linking to that single one. Please see below CMakeLists of my failed attempt.

cmake_minimum_required(VERSION 3.11)
project(modules)

# 10 libraries with actually functionality
add_subdirectory(mylib1)
add_subdirectory(mylib2)
...
add_subdirectory(mylib10)

# failed attempt to create a single library that links to the above 10
add_library(myliball)

target_link_libraries(myliball mylib1 mylib2 ... mylib10)

install(TARGETS myliball
        EXPORT ${CMAKE_PROJECT_NAME}Targets
        ARCHIVE DESTINATION lib
        LIBRARY DESTINATION lib
        RUNTIME DESTINATION bin)

export(TARGETS myliball
      APPEND FILE ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}Targets.cmake)

When I run cmake it shows this error

No SOURCES given to target: myliball

I can probably create an empty class for myliball to workaround this problem but that seems to be very messy. Is there a better way to do this?

1 Answers

CMake has a special type of library target which is intended for grouping - INTERFACE:

add_library(myliball INTERFACE)

target_link_libraries(myliball INTERFACE mylib1 mylib2 ... mylib10)

Such library target is not compiled, it just serves for propagate its INTERFACE properties when linked.

Related