Cmake: Exporting subproject targets to main project

瘦欲@ 提交于 2019-12-03 14:29:32

As noted in the bugreport you refer to install() command should be issued from the same directory where target is created. As you have libraries target created in different directories, you need to assign different export names for them, and, consequently, different export files.

But you are free to include both export files into the LIBSConfig.cmake script:

cmake/LIBSConfig.cmake:

get_filename_component(SELF_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
include(${SELF_DIR}/LIBS-lib1.cmake)
include(${SELF_DIR}/LIBS-lib2.cmake)

lib1/CMakeLists.txt:

add_library(lib1 ...)
install(TARGET lib1 EXPORT lib1-export ...)

lib2/CMakeLists.txt:

add_library(lib2 ...)
install(TARGET lib2 EXPORT lib2-export ...)

CMakeLists.txt:

add_subdirectory(lib1)
add_subdirectory(lib2)

install(EXPORT lib1-export FILENAME LIBS-lib1.cmake DESTINATION lib/LIBS)
install(EXPORT lib2-export FILENAME LIBS-lib2.cmake DESTINATION lib/LIBS)
install(FILES cmake/LIBSConfig.cmake DESTINATION lib/LIBS)

Note, that export command exports build tree. It is usually not suitable for find_package, which is normally used for find installed files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!