简体   繁体   中英

How to specify the path where CMake is installed in the CMakeLists.txt

I downloaded the portable CMake version from the official site, and installed it in my home directory( ~/usr ) because I don't have root or sudo permission.

How do I specify the path where CMake is installed in CMakeLists.txt, such as ~/usr/cmake-path/bin/cmake ?

Note : I don't want to use the default version of CMake on the Linux system for it's too old (version 2.6)

PS:

I know of the variable CMAKE_COMMAND which may be useful for my question, but I don't know how to use it!

I feel a more robust solution is to prepend your cmake path to the PATH environment variable.

export PATH=~/usr/cmake-path/bin:$PATH

If your on an Ubuntu/Debian system you can add this command to your ~/.bashrc to execute it with every terminal session. Note, this change will only affect your account and you can set it without administrator permissions.

This way you only need to type cmake and the desired version will be found first.

CMake will use whatever path the running CMake executable is in. Furthermore, it may get confused if you switch paths between runs without clearing the cache.

So what you have to do is simply instead of running cmake <path_to_src> from the command line, run ~/usr/cmake-path/bin/cmake <path_to_src> .

You may want to add an alias or a shell script to the path that is a little more typeable (so you only have to type my_cmake <path_to_src> or something like that).

Note that there is no clean way to solve this by just editing the CMakeLists.txt . While in theory you could have CMake 2.6 run an outer CMake script that does nothing but running an inner CMake script from a 3.0 executable, that's just a dirty hack. Just run the correct executable from the command line and you should be fine.

best advice is to install cmake in a sandbox environment rather than on your own, this is the entire environment is preserved while benefiting in increased flexibility. cmake depends on this for existence otherwise your environment becomes a 'version hell'.

You set the install directory using the flag CMAKE_INSTALL_PREFIX during your CMake build as follows:

cmake path/to/sourcedir \
    -DCMAKE_INSTALL_PREFIX=path/to/install \

Don't forget to precede any CMAKE_FLAGS with -D .

If you want to include a custom install prefix in your CMakeLists.txt , then you can include the following:

SET(MY_INSTALL_PREFIX "/path/to/install" CACHE PATH "Prefix added to install directories")
SET(CMAKE_INSTALL_PREFIX "${MY_INSTALL_PREFIX}" CACHE INTERNAL "Prefix added to install directories" FORCE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM