You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CMake教程Step4安装异常:为何安装目录需CMakeLists.txt?

问题

在跟随CMake官方教程Step4的安装环节时,执行安装命令出现错误,提示安装目录不存在CMakeLists.txt。具体执行流程及错误如下:

执行的命令序列:

(base) xxx@xxx-ThinkPad-T470p: $ cmake ../Step4 -DUSE_MYMATH=ON
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xxx/work/CMake_examples/cmake-3.24.1-tutorial-source/Step4_build
(base) xxx@xxx-ThinkPad-T470p: $ cmake --build .
[ 50%] Built target MathFunctions
[100%] Built target Tutorial
(base) xxx@xxx-ThinkPad-T470p: $ cmake --install . --prefix /tmp/testinstall
CMake Error: The source directory "/tmp/testinstall" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.

使用的CMake版本:

(base) xxx@xxx-ThinkPad-T470p: $ cmake --version
cmake version 3.10.2

主项目CMakeLists.txt内容:

cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)

# add the MathFunctions library
if(USE_MYMATH)
  add_subdirectory(MathFunctions)
  list(APPEND EXTRA_LIBS MathFunctions)
endif()

# add the executable
add_executable(Tutorial tutorial.cxx)

target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )

install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  DESTINATION include
  )

MathFunctions库的CMakeLists.txt内容:

add_library(MathFunctions mysqrt.cxx)

# state that anybody linking to us needs to include the current source dir
# to find MathFunctions.h, while we don't.
target_include_directories(MathFunctions
          INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
          )

install(TARGETS MathFunctions DESTINATION lib)
install(FILES MathFunctions.h DESTINATION include)

原因及解决方法

错误原因

cmake --install是CMake 3.15版本才引入的新命令语法,而你使用的3.10.2版本不支持该命令。旧版本会将--prefix /tmp/testinstall误判为要处理的源目录参数,因此去检查该目录下的CMakeLists.txt,最终抛出错误。

解决方法

有两种可行方案:

  1. 使用构建工具原生安装命令
    若使用默认的Makefile生成器,执行以下命令完成安装:

    make install DESTDIR=/tmp/testinstall
    

    注:DESTDIR会作为前缀添加到CMakeLists.txt中指定的DESTINATION路径前,最终安装路径为/tmp/testinstall/bin、/tmp/testinstall/lib等。

  2. 升级CMake版本
    将CMake升级到3.15或更高版本,即可直接使用教程中的cmake --install . --prefix /tmp/testinstall命令完成安装。


内容的提问来源于stack exchange,提问作者sigirisetti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 23:05:29