Issue
I'm following this CMake tutorial : https://cmake.org/cmake/help/latest/guide/tutorial/index.html At Step 4, https://cmake.org/cmake/help/latest/guide/tutorial/Installing%20and%20Testing.html install is not working as expected. Not sure what is done wrongly by me
I made changes suggested to CMakeLists.txt and from build dir I ran config, build and then tried to install using prefix. Install is giving below error
(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.
Why is cmake expecting CMakeLists.txt in install directory?
Library 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)
Main 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 Version output
(base) xxx@xxx-ThinkPad-T470p: $ cmake --version
cmake version 3.10.2
Solution
Your cmake is too old for the --install
command syntax.
--install
was added to CMake in version 3.15 so you need to install a newer version.
CMake version 3.20 greatly improved handling unknown option arguments (see issue tracker entry). Older versions tried to handle one of the arguments as your sources directory. That's why you see the error message complaining about a missing CMakeLists.txt file in your install directory.
Answered By - vre Answer Checked By - Willingham (WPSolving Volunteer)