cmake 外部项目命令似乎忽略 INSTALL_DIR

cmake external projects command seems to ignore INSTALL_DIR

首先,我对 cmake 比较陌生。我正在尝试使用 cmake 构建具有单个外部依赖项的项目。我将外部项目的 INSTALL_DIR 指定为 CMAKE_INSTALL_PREFIX,因此它将安装到与父项目相同的位置。但是当我 运行 make 时,它​​会忽略它并尝试安装到 /usr/local/lib.

这是我的 CMakeList.txt:

cmake_minimum_required( VERSION 2.8 )
include( ExternalProject )
project( capture )
add_library( capture SHARED capture.cc )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )

ExternalProject_Add( proj_exceptions 
    GIT_REPOSITORY /home/user/workspace/exceptions 
    INSTALL_DIR ${CMAKE_INSTALL_PREFIX} 
)

add_library( exceptions SHARED IMPORTED )
set_property( TARGET exceptions 
    PROPERTY IMPORTED_LOCATION ${CMAKE_INSTALL_PREFIX}/lib/libexceptions.so 
)
add_dependencies( exceptions proj_exceptions )
include_directories( ${CMAKE_INSTALL_PREFIX}/include )
target_link_libraries( capture exceptions )
install( TARGETS capture DESTINATION lib )
install( FILES capture.h DESTINATION include )

CMakeLists.txt 外部项目如下所示:

cmake_minimum_required( VERSION 2.8 )
project( exceptions )
add_library( exceptions SHARED exceptions.cc )
install( TARGETS exceptions DESTINATION lib )
install( FILES exceptions.hh DESTINATION include )

它很好地克隆并构建了外部项目,但在安装步骤时却卡住了:

Install the project...
-- Install configuration: ""
-- Installing: /usr/local/lib/libexceptions.so
CMake Error at cmake_install.cmake:42 (file):
  file INSTALL cannot copy file
  "/home/user/workspace/capture/build/proj_exceptions-prefix/src/proj_exceptions-build/libexceptions.so"
  to "/usr/local/lib/libexceptions.so".
Makefile:66: recipe for target 'install' failed

如您所见,安装配置为空。查看为外部项目生成的配置,我在 cmake_install.cmake:

中找到了这个
if(NOT DEFINED CMAKE_INSTALL_PREFIX)
  set(CMAKE_INSTALL_PREFIX "/usr/local")
endif()

所以,似乎将 INSTALL_DIR 传递给 ExternalProject_Add 并没有设置安装前缀。安装步骤成功,如果我改为使用:

ExternalProject_Add( proj_exceptions 
    GIT_REPOSITORY /home/djones/workspace/exceptions 
    CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" 
)     

那么INSTALL_DIR的目的是什么?

您的目的是 INSTALL_DIR,但您可能错过了一些步骤。

根据cmake 2.8 doc about external project

Install Step

The INSTALL_DIR is underneath the calling project’s binary directory. Use INSTALL_DIR to specify a different location. Note that in addition to setting INSTALL_DIR, you also have to pass -DCMAKE_INSTALL_PREFIX or --prefix to the CMake or configure command. It is not used automatically in the configure step since not all projects follow this convention.

# [INSTALL_DIR dir]

You can refer to the install directory in your configure command, for example:

CONFIGURE_COMMAND SOURCE_DIR/configure --prefix=INSTALL_DIR

 # [INSTALL_COMMAND cmd...]

CMake-based projects use ‘cmake--build’ to build the install target. Other projects use ‘make install’. Use INSTALL_COMMAND to customize the install step. Use INSTALL_COMMAND “” to omit the install step. The install command executes with the working directory set to .

因此请尝试更新您的 cmake 命令,或使用自定义 INSTALL_COMMAND 功能。