首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CMake教程第4步安装示例未按预期工作

CMake教程第4步安装示例未按预期工作
EN

Stack Overflow用户
提问于 2022-09-06 14:43:52
回答 1查看 87关注 0票数 0

我正在学习本CMake教程:https://cmake.org/cmake/help/latest/guide/tutorial/index.html在第4步,https://cmake.org/cmake/help/latest/guide/tutorial/Installing%20and%20Testing.html安装不像预期的那样工作。不知道我做错了什么

我对CMakeLists.txt进行了修改,并从build中运行了配置、构建,然后尝试使用前缀进行安装。安装出现以下错误

代码语言:javascript
运行
复制
(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需要安装目录中的CMakeLists.txt?

库CMakeLists.txt

代码语言:javascript
运行
复制
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)

主CMakeLists.txt

代码语言:javascript
运行
复制
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
  )                           

Cmake版本输出

代码语言:javascript
运行
复制
(base) xxx@xxx-ThinkPad-T470p: $ cmake --version
cmake version 3.10.2
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-07 06:35:10

您的cmake对于--install命令语法来说太老了。

--install是在3.15版本中添加到CMake中的,因此您需要安装一个更新的版本。

CMake版本3.20极大地改进了处理未知选项参数(参见问题跟踪器条目)。旧版本试图将其中一个参数作为源目录处理。这就是为什么您看到错误消息抱怨安装目录中缺少一个CMakeLists.txt文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73623915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档