删除CMake中的install target



我有一个cmake管理的项目,其中永远在生成Makefile后执行make install是明智的。此外,如果用户键入make install并将头文件放在/usr/local/include的默认CMAKE_INSTALL_PREFIX中,那么将来的构建可能会受到污染。

我想指示cmake在生成的Makefile中生成install目标。这可能吗?

注意:与此接近的解决方案是mark_as_advanced(CMAKE_INSTALL_PREFIX),但只有隐藏了ccmake选项。也许是set(CMAKE_INSTALL_PREFIX /dont/do/this)?

在顶级CMakeLists.txt的开头设置CMAKE_SKIP_INSTALL_RULES为ON(并注释解释原因)。正如文档中所说:

如果TRUE, CMake既不生成安装规则,也不生成cmake_install.cmake文件。该变量默认为FALSE

一个快速的交互来证明这是有效的:

$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(test)
set(CMAKE_SKIP_INSTALL_RULES YES)
add_executable(app main.cpp)
$ cmake -S . -B build
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build
$ cmake --build build/ --target install
ninja: error: unknown target 'install'

尝试调用install()将发出以下警告:

CMake Warning in CMakeLists.txt:
CMAKE_SKIP_INSTALL_RULES was enabled even though installation rules have
been specified

最新更新