pabloariasal / modern-cmake-sample

Example library that shows best practices and proper usage of CMake by using targets
MIT License
657 stars 72 forks source link

Exporting targets of library in case of subdirectories #6

Open CDitzel opened 6 years ago

CDitzel commented 6 years ago

What if the lib itself would be structured into several subprojects by means of the add_subdirectory() command in the top-level CMakeLists.txt. Can you maybe show, how to proceed then? Where do the exporting and installation commands go? into every individual subproject or do you define them all together in the root cmake file? Daniel Pfeiffer is not clear on this as well and I would appreciate it, if you could shed some light on this issue

ccvca commented 6 years ago

imho install is done in each subdirectory

install(TARGETS SUB_DIR_TARGETS
    EXPORT jsonutils-export
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

At the TopLevel, the install-EXPORT is done (once):

install(EXPORT jsonutils-targets
  FILE
    JSONUtilsTargets.cmake
  NAMESPACE
    JSONUtils::
  DESTINATION
    ${CMAKE_INSTALL_LIBDIR}/cmake/JSONUtils
)

(CMake parts copied from https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/)