从 CMakeLists.txt 文件设置 CMAKE_INSTALL_PREFIX [英] Setting CMAKE_INSTALL_PREFIX from CMakeLists.txt file
问题描述
如何在我的根 CMakeLists.txt 文件中设置 CMAKE_INSTALL_PREFIX?
How do I set CMAKE_INSTALL_PREFIX in my root CMakeLists.txt file?
我一直在做
cmake_minimum_required(VERSION 2.8)
project(MyProject)
# Set default install prefix
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})
希望通过安装将注定到源代码树中的文件夹.也就是说,
with the hopes that by installations would be destined to folders in the source tree. That is,
install(TARGETS my_exe DESTINATION bin/)
将安装到 ${CMAKE_SOURCE_DIR}/bin/
.相反,它不断尝试写入 /usr/local/bin
(Ubuntu 14.04 的默认设置).
would install to ${CMAKE_SOURCE_DIR}/bin/
. Instead, it keeps trying to write to /usr/local/bin
(the default for Ubuntu 14.04).
我尝试了这个问题的答案,但我仍然得到当我检查 CMakeCache.txt 时,标准 usr/local/
作为我的 CMAKE_INSTALL_PREFIX.
I tried the answers to this question, but I still get the standard usr/local/
as my CMAKE_INSTALL_PREFIX when I check CMakeCache.txt.
我唯一可行的解决方案是做
The only working solution I have is to do
install(TARGETS my_exe DESTINATION "${CMAKE_SOURCE_DIR}/bin/")
但这会消除用户指定要安装的 bin 目录位置的能力.
but this then removes the user's ability to specify where the bin directory to install is.
tl;dr 我希望 make install
默认自动安装到 ${CMAKE_SOURCE_DIR}
,而不是 /usr/local/
.
tl;dr I would like make install
to automatically install to ${CMAKE_SOURCE_DIR}
by default, rather than /usr/local/
.
推荐答案
CMake 开发人员 建议 使用给定的模式更改 CMAKE_INSTALL_PREFIX 中 CMakeLists.txt
的默认值:
CMake developers suggest to use given pattern for change default value of CMAKE_INSTALL_PREFIX inside CMakeLists.txt
:
# Use this snippet *after* PROJECT(xxx):
IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
SET(CMAKE_INSTALL_PREFIX <path> CACHE PATH <comment> FORCE)
ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
<小时>
使用这种方法
Using that approach
# Use this snippet *before* PROJECT(xxx):
SET(CMAKE_INSTALL_PREFIX <path> CACHE PATH <comment>)
不推荐:
.. 解决方案取决于 PROJECT 命令的实现细节,并且非常脆弱,因为它在某些版本的 CMake 中意外"工作.我根本不认为这是一种选择.
.. solution depends on the implementation details of the PROJECT command and is very fragile since it works "accidentally" for some versions of CMake. I don't consider it to be an option at all.
这篇关于从 CMakeLists.txt 文件设置 CMAKE_INSTALL_PREFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!