如何在CMake中将多个库目标分组为一个

时间:2019-06-12 18:45:01

标签: cmake

我正在尝试将多个目标分组为一个目标,因此下游用户只需要链接到该目标即可。下游用户无需查找所有目标,并且可以通过链接到单个库来获得上游库中的所有功能。请在下面的CMakeList中查看我失败的尝试。

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)

当我运行cmake时,它显示此错误

No SOURCES given to target: myliball

我可能可以为myliball创建一个空类来解决此问题,但这似乎非常混乱。有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

CMake具有特殊类型的库目标,该目标用于分组-接口

add_library(myliball INTERFACE)

target_link_libraries(myliball INTERFACE mylib1 mylib2 ... mylib10)

此类库目标未编译,仅用于在链接时传播其INTERFACE属性。