修改子目录的CMAKE_INSTALL_PREFIX

时间:2017-09-13 06:13:59

标签: cmake install

我有一个应该安装在${CMAKE_INSTALL_PREFIX}(由用户提供)的项目。

我包含一个add_subdirectory(subproject)的子项目。此子项目将内容安装到${CMAKE_INSTALL_PREFIX},但我宁愿将其安装到${CMAKE_INSTALL_PREFIX}/subproject。如何在不触及子项目的情况下做到这一点?

我试过这个:但它没有效果:

set( _INSTALL_PREFIX_BACKUP ${CMAKE_INSTALL_PREFIX})
set( CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/subproject)
add_subdirectory(subproject)
set( CMAKE_INSTALL_PREFIX ${_INSTALL_PREFIX_BACKUP})

我也试过这个:

include(ExternalProject)
ExternalProject_Add( subproject
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/subproject
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}/subproject"
    INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/subproject"
)

2 个答案:

答案 0 :(得分:1)

关于install()功能的全部内容。

在子项目的CMakeLists.txt中,您需要以下一行:

install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION subproject)

根据您的用例,您可以将LIBRARY调整为RUNTIME subproject这是路径。它与${CMAKE_INSTALL_PREFIX}相关。

请在此处documentation作为参考。

答案 1 :(得分:1)

这是我提出的解决方案。在子项目中:

if (SUBPROJECT_INSTALL_PREFIX)
  set(_SUBPROJECT_INSTALL_PREFIX ${SUBPROJECT_INSTALL_PREFIX}/ )
else()
  set(_SUBPROJECT_INSTALL_PREFIX . )
endif()

install(TARGETS ${PROJECT_NAME}
        RUNTIME DESTINATION ${_SUBPROJECT_INSTALL_PREFIX}/bin
        ARCHIVE DESTINATION ${_SUBPROJECT_INSTALL_PREFIX}/lib/static
        LIBRARY DESTINATION ${_SUBPROJECT_INSTALL_PREFIX}/lib)

在超级项目中,我然后使用:

set(SUBPROJECT_INSTALL_PREFIX "path/to/subproject" CACHE STRING "Path in project")
add_subdirectory(subproject)