开发者

How to properly set CMAKE_INSTALL_PREFIX from the command-line

I want to开发者_运维问答 generate a Makefile with an install target, making installation to /usr instead of default /usr/local. Assuming that the build directory is a subdirectory of the source directory, I execute:

cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr ..

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

Now I execute:

make
make install

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

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

install(TARGETS mylibrary DESTINATION lib)


That should be (see the docs):

cmake -DCMAKE_INSTALL_PREFIX=/usr ..


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 BEFORE PROJECT(< project_name>) command, otherwise it will not work!


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

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 after setting project is the better place.

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.


This worked for me:

Place this code to the cmakelist.txt at the very top:

SET(CMAKE_INSTALL_PREFIX < install_path >)

For some reason, doing it via the command line does not work for me:

-D CMAKE_INSTALL_PREFIX=/home/user/Human-Detection-Module/build/install/


In modern CMake CLI, it's even easier. The last command installs the targets in build folder to myInstallationDirectory using the prefix option.

cmake -S . -B build
cmake --build build
cmake --install build --prefix myInstallationDirectory

Reference: https://cmake.org/cmake/help/latest/manual/cmake.1.html?highlight=install#cmdoption-cmake--install-0


For example, to add exteranl projects:

The following works for me (cmake for windows: v3.25.2) based on this thread:

project("My_Project" VERSION 1.0.0 LANGUAGES C CXX)

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    
    set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/external 
        CACHE PATH "Force path to set CMAKE_INSTALL_PREFIX" FORCE)

endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)

To verify, run $> ccmake build or $> ccmake . in the build directory to see the cache variable set to:

 CMAKE_BUILD_TYPE                 Debug                                                                                     
 CMAKE_CONFIGURATION_TYPES        Debug;Release;MinSizeRel;RelWithDebInfo                                                   
 CMAKE_EXPORT_COMPILE_COMMANDS    ON                                                                                        
 CMAKE_INSTALL_PREFIX             D:/Projects/CMakeTest/external 

Previously it was:

 CMAKE_BUILD_TYPE                 Debug                                                                                     
 CMAKE_CONFIGURATION_TYPES        Debug;Release;MinSizeRel;RelWithDebInfo                                                   
 CMAKE_EXPORT_COMPILE_COMMANDS    ON                                                                                        
 CMAKE_INSTALL_PREFIX             C:/Program Files/My_Project

This is on windows, I reckon that it would be /usr/cmake/My_project on linux.(if someone could confirm)

EDIT:

If the changes do not reflect, you can delete the CMakeCache.txt

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜