4

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?

user3667089
  • 2,996
  • 5
  • 30
  • 56
  • 1
    You should probably call targets "static libraries" – drescherjm Jun 12 '19 at 18:48
  • This may be a duplicate: https://stackoverflow.com/questions/37924383/combining-several-static-libraries-into-one-using-cmake – drescherjm Jun 12 '19 at 18:49
  • No, it's not just static libraries. "Targets" includes information such as `INTERFACE_COMPILE_DEFINITIONS`, `INTERFACE_COMPILE_FEATURES`, `INTERFACE_INCLUDE_DIRECTORIES` and `INTERFACE_LINK_LIBRARIES` – user3667089 Jun 12 '19 at 18:51
  • @drescherjm the one you linked is different, this should work for dynamic libraries and there isn't a proper solution for their problem either. – user3667089 Jun 12 '19 at 18:55
  • @Tsyvarev updated the title as you suggested – user3667089 Jun 12 '19 at 20:27

1 Answers1

5

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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153