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

 2021-01-14 

cmake external projects command seems to ignore INSTALL_DIR

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

这是我的CMakeList.txt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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看起来像这样:

1
2
3
4
5
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 )

它很好地克隆并构建了外部项目,但是在安装步骤上却很麻烦:

1
2
3
4
5
6
7
8
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:

中找到了它

1
2
3
if(NOT DEFINED CMAKE_INSTALL_PREFIX)
  set(CMAKE_INSTALL_PREFIX"/usr/local")
endif()

因此,似乎将INSTALL_DIR传递给ExternalProject_Add并没有设置安装前缀。如果我改为使用:

,则安装步骤成功

1
2
3
4
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文档:

Install Step

The INSTALL_DIR is underneath the calling projecta€?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:

1
2
3
CONFIGURE_COMMAND SOURCE_DIR/configure --prefix=INSTALL_DIR

 # [INSTALL_COMMAND cmd...]

CMake-based projects use a€?cmake--builda€? to build the install target.
Other projects use a€?make installa€?. Use INSTALL_COMMAND to customize
the install step. Use INSTALL_COMMAND a€?a€? to omit the install step. The
install command executes with the working directory set to
.

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