使用cmake构建外部项目时如何不进行安装步骤

How to not make install step when building external project with cmake

我正在使用 cmake ExternalProject_Add 命令构建依赖项目:

include(ExternalProject)
...
set(COMMON_BASE_PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../CommonBase)

ExternalProject_Add(CommonBaseProject
  SOURCE_DIR ${COMMON_BASE_PROJECT_DIR}
  BINARY_DIR ${COMMON_BASE_PROJECT_DIR}/build
  INSTALL_COMMMAND ""
)   

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${COMMON_BASE_PROJECT_DIR}/include)

add_library(
    ${LIBRARY_NAME}
    SHARED
    ${SRC_FILES}
    ${INCLUDE_FILES}
)

target_link_libraries (Bios ${COMMON_BASE_PROJECT_DIR}/build/libCommonBase.dll)
add_dependencies(Bios CommonBaseProject)

但我收到错误:

[100%] Linking CXX shared library libCommonBase.dll
[100%] Built target CommonBase
[ 50%] Performing install step for 'CommonBaseProject'
make[3]: *** No rule to make target 'install'.  Stop.

我不需要进行安装步骤,所以我的问题是:如何禁用它?

您可以使用 STEP_TARGETS build 为构建步骤生成一个目标,并添加对该特定目标的依赖。步骤目标被命名为 <external-project-name>-<step-name> 因此在这种情况下代表构建步骤的目标将被命名为 CommonBaseProject-build.

您可能还想使用 EXCLUDE_FROM_ALL TRUE 从 "all" 目标中排除 CommonBaseProject。

ExternalProject_Add(CommonBaseProject
  SOURCE_DIR ${COMMON_BASE_PROJECT_DIR}
  BINARY_DIR ${COMMON_BASE_PROJECT_DIR}/build
  STEP_TARGETS build
  EXCLUDE_FROM_ALL TRUE
)

add_dependencies(Bios CommonBaseProject-build)

你几乎成功了:而不是 INSTALL_COMMAND "" 放一些类似

的东西
    INSTALL_COMMAND cmake -E echo "Skipping install step."

与您的问题无关,该问题已得到解答,但就我而言,我有以下 ExternalProject_Add 指令:

ExternalProject_Add(external_project
    # [...]
    # Override build/install command
    BUILD_COMMAND ""
    INSTALL_COMMAND
        "${CMAKE_COMMAND}"
        --build .
        --target INSTALL    # Wrong casing for "install" target
       --config ${CMAKE_BUILD_TYPE}
)

在这种情况下,cmake 退出并出现非常相似的错误 (*** No rule to make target 'INSTALL'),但在这种情况下,外部项目正在寻找不正确的大写 INSTALL 目标:正确的大小写是 install 反而。显然,这在 Windows 中与 MSVC 一起工作但在 unix 操作系统中失败。

从至少 CMake 3.10 开始,空字符串足以抑制安装步骤:

Passing an empty string as the <cmd> makes the install step do nothing.

其他阶段也是如此;有关更多信息,请参阅 docs

如果您仍在使用 CMake <3.10 进行构建,那么您需要更新 CMake ;)