如何使用CMAKE_INSTALL_PREFIX


97

我想生成带有安装目标的Makefile,将其安装到/ usr而不是默认的/ usr / local。假设构建目录在源子目录中完成,我执行:

cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr ..

CMakeCache.txt包含:(CMAKE_INSTALL_PREFIX:PATH=/usr确定?)

现在我执行:

使
进行安装

所有文件仍安装在usr / local中。怎么了?

编辑:在任何CMakeLists.txt项目文件中没有CMAKE_INSTALL_PREFIX。在运行cmake之前,我将从输出目录中删除所有内容。CMakeLists.txt中的安装指令如下所示:

install(TARGETS mylibrary DESTINATION lib)

Answers:


120

应该是(请参阅docs):

cmake -DCMAKE_INSTALL_PREFIX=/usr ..

30

有两种使用此变量的方法:

  • 像Job提到的那样将其作为命令行参数传递:

    cmake -DCMAKE_INSTALL_PREFIX=< install_path > ..

  • 给它赋值CMakeLists.txt

    SET(CMAKE_INSTALL_PREFIX < install_path >)

    但是请记住要在 PROJECT(< project_name>)命令之前放置它,否则它将不起作用!


4
奇怪的是,仅当我将SET()语句放在PROJECT()语句之后(CMake 2.8)之后,SET()语句才对我有用。
AstroFloyd 2015年

2
该答案及其链接的参考文献更直接地讨论了project()命令之前/之后。
Craig Scott

这是什么PROJECT命令,以及如何获得此cmake选项?你有一个例子吗?
Stephane

6

但是请记住将其放置在PROJECT(<project_name>)命令之前,否则它将不起作用!

我使用cmake的第一周-在使用了几年的GNU自动工具之后-所以我仍然在学习(最好写m4宏),但是我认为设置项目修改CMAKE_INSTALL_PREFIX 是更好的地方。

CMakeLists.txt

cmake_minimum_required (VERSION 2.8)

set (CMAKE_INSTALL_PREFIX /foo/bar/bubba)
message("CIP = ${CMAKE_INSTALL_PREFIX} (should be /foo/bar/bubba")
project (BarkBark)
message("CIP = ${CMAKE_INSTALL_PREFIX} (should be /foo/bar/bubba")
set (CMAKE_INSTALL_PREFIX /foo/bar/bubba)
message("CIP = ${CMAKE_INSTALL_PREFIX} (should be /foo/bar/bubba")

首次运行(无缓存)

CIP = /foo/bar/bubba (should be /foo/bar/bubba
-- The C compiler identification is GNU 4.4.7
-- etc, etc,...
CIP = /usr/local (should be /foo/bar/bubba
CIP = /foo/bar/bubba (should be /foo/bar/bubba
-- Configuring done
-- Generating done

第二次

CIP = /foo/bar/bubba (should be /foo/bar/bubba
CIP = /foo/bar/bubba (should be /foo/bar/bubba
CIP = /foo/bar/bubba (should be /foo/bar/bubba
-- Configuring done
-- Generating done

如果我弄错了,请告诉我,我有很多学习要做。好有趣。


设置时尝试使用FORCE:set(CMAKE_INSTALL_PREFIX / foo / bar / bubba CACHE PATH“ Cmake prefix” FORCE)
Jav_Rock
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.