Linux 如何使用 CMAKE_INSTALL_PREFIX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6241922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:02:17  来源:igfitidea点击:

How to use CMAKE_INSTALL_PREFIX

linuxinstallationcmake

提问by Alex F

I want to generate Makefile with install target, making installation to /usr instead of default /usr/local. Assuming that build directory is done in the source subdirectory, I execute:

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

cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr ..

cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr ..

CMakeCache.txt contains: CMAKE_INSTALL_PREFIX:PATH=/usr(OK?)

CMakeCache.txt 包含:(CMAKE_INSTALL_PREFIX:PATH=/usr好吗?)

Now I execute:

现在我执行:

make
make install

All files are still installed to usr/local. What is wrong?

所有文件仍然安装到 usr/local。怎么了?

Edit: There is no CMAKE_INSTALL_PREFIX in any of CMakeLists.txt project files. Before running cmake, I delete everything from the output directory. install directives in CMakeLists.txt look like:

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

install(TARGETS mylibrary DESTINATION lib)

install(TARGETS mylibrary DESTINATION lib)

采纳答案by Job

That should be (see the docs):

那应该是(见文档):

cmake -DCMAKE_INSTALL_PREFIX=/usr ..

回答by ryan_tu

There are two ways to use this variable:

有两种方法可以使用这个变量:

  • passing it as a command line argument just like Job mentioned:

    cmake -DCMAKE_INSTALL_PREFIX=< install_path > ..

  • assigning value to it in CMakeLists.txt:

    SET(CMAKE_INSTALL_PREFIX < install_path >)

    But do remember to place it BEFOREPROJECT(< project_name>)command, otherwise it will not work!

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

    cmake -DCMAKE_INSTALL_PREFIX=< install_path > ..

  • 为它赋值CMakeLists.txt

    SET(CMAKE_INSTALL_PREFIX < install_path >)

    但是一定要记得把它放在PROJECT(< project_name>)命令之前,否则它不会工作!

回答by Jim

But do remember to place it BEFORE PROJECT(< project_name>) command, otherwise it will not work!

但是一定要记得把它放在 BEFORE PROJECT(<project_name>) 命令之前,否则它不会工作!

My first week of using cmake - after some years of GNU autotools - so I am still learning (better then writing m4 macros), but I think modifying CMAKE_INSTALL_PREFIX aftersetting project is the better place.

我使用 cmake 的第一周 - 经过几年的 GNU autotools - 所以我仍在学习(比编写 m4 宏更好),但我认为设置项目修改 CMAKE_INSTALL_PREFIX是更好的地方。

CMakeLists.txt

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")

First run (no cache)

首次运行(无缓存)

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

Second run

第二次运行

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

Let me know if I am mistaken, I have a lot of learning to do. It's fun.

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