我正在学习本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中运行了配置、构建,然后尝试使用前缀进行安装。安装出现以下错误
(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
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
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版本输出
(base) xxx@xxx-ThinkPad-T470p: $ cmake --version
cmake version 3.10.2
发布于 2022-09-07 06:35:10
您的cmake对于--install
命令语法来说太老了。
--install
是在3.15版本中添加到CMake中的,因此您需要安装一个更新的版本。
CMake版本3.20极大地改进了处理未知选项参数(参见问题跟踪器条目)。旧版本试图将其中一个参数作为源目录处理。这就是为什么您看到错误消息抱怨安装目录中缺少一个CMakeLists.txt文件。
https://stackoverflow.com/questions/73623915
复制相似问题