从CMakeLists.txt文件设置CMAKE_INSTALL_PREFIX [英] Setting CMAKE_INSTALL_PREFIX from CMakeLists.txt file

查看:843
本文介绍了从CMakeLists.txt文件设置CMAKE_INSTALL_PREFIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在根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时,将c $ c> 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开发人员建议使用给定的模式进行更改 CMakeLists.txt 中的 CMAKE_INSTALL_PREFIX 默认值:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆