Installing CMake {安装 CMake}
1. CMake Error at CMakeLists.txt:1 (cmake_minimum_required): CMake 3.22 or higher is required.
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ ls
CMakeLists.txt cmake-build-debug main.cpp
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ cat main.cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(yongqiang)
set(CMAKE_CXX_STANDARD 14)
add_executable(yongqiang main.cpp)
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ mkdir build
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ cd build/
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang/build$ cmake ..
CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
CMake 3.22 or higher is required. You are running version 3.16.3
-- Configuring incomplete, errors occurred!
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang/build$
2. conda install cmake
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ cmake --version
cmake version 3.16.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ conda install cmake
Retrieving notices: done
Channels:
- defaults
- pytorch
Platform: linux-64
Collecting package metadata (repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /home/yongqiang/miniconda3
added / updated specs:
- cmake
The following packages will be downloaded:
package | build
---------------------------|-----------------
cmake-3.26.4 | h96355d8_0 16.7 MB defaults
conda-24.11.1 | py311h06a4308_0 1.2 MB defaults
libuv-1.48.0 | h5eee18b_0 950 KB defaults
rhash-1.4.3 | hdbd6064_0 220 KB defaults
------------------------------------------------------------
Total: 19.1 MB
The following NEW packages will be INSTALLED:
cmake anaconda/pkgs/main/linux-64::cmake-3.26.4-h96355d8_0
libuv anaconda/pkgs/main/linux-64::libuv-1.48.0-h5eee18b_0
rhash anaconda/pkgs/main/linux-64::rhash-1.4.3-hdbd6064_0
The following packages will be UPDATED:
conda 24.11.0-py311h06a4308_0 --> 24.11.1-py311h06a4308_0
Proceed ([y]/n)? y
Downloading and Extracting Packages:
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ cmake --version
cmake version 3.26.4
CMake suite maintained and supported by Kitware (kitware.com/cmake).
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ ls
CMakeLists.txt cmake-build-debug main.cpp
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ mkdir build
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ cd build
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang/build$ cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (7.3s)
-- Generating done (0.0s)
-- Build files have been written to: /home/yongqiang/CLionProjects/yongqiang/build
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang/build$ make
[ 50%] Building CXX object CMakeFiles/yongqiang.dir/main.cpp.o
[100%] Linking CXX executable yongqiang
[100%] Built target yongqiang
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang/build$ ls
CMakeCache.txt CMakeFiles Makefile cmake_install.cmake yongqiang
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang/build$ ./yongqiang
Hello, World!
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang/build$
3. Installing CMake (安装 CMake)
Installing CMake
https://cliutils.gitlab.io/modern-cmake/chapters/intro/installing.html
https://modern-cmake-cn.github.io/Modern-CMake-zh_CN/chapters/intro/installing.html
New versions work better for everyone.
3.1. pip install cmake
https://pypi.org/project/cmake/
If you have pip
(Python’s package installer), you can do:
pip install cmake
Personally, on Linux, I put versions of CMake in folders, like /opt/cmake312
or ~/opt/cmake312
.
就我个人而言,在 Linux 上时,我会把 CMake 的版本放入文件夹名中,比如 /opt/cmake312
或 ~/opt/cmake312
。
EnvModule Setup
https://github.com/CLIUtils/envmodule_setup
3.2. Official package
You can download CMake from KitWare. This is how you will probably get CMake if you are on Windows.
你可以从 KitWare 上下载 CMake。如果你是在 Windows 上,这可能就是你获得 CMake 的方式。
KitWare
https://cmake.org/download/
You can also get it on most other package managers, such as Chocolatey for Windows or MacPorts for macOS.
Windows 的 Chocolatey 或 macOS 的 MacPorts
Chocolatey
https://chocolatey.org/
The Package Manager for Windows
MacPorts
https://www.macports.org/
The MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the Mac operating system.
It’s not a bad way to get it on macOS either (and a Universal2 version is supplied supporting both Intel and Apple Silicon), but using brew install cmake
is much nicer if you use Homebrew (and you should; Apple even supports Homebrew such as during the Apple Silicon rollout).
在 macOS 上获得它的方法也不错,但如果你使用 Homebrew 的话,使用 brew install cmake
会带来更好的效果。
Homebrew
https://brew.sh/
The Missing Package Manager for macOS (or Linux)
On Linux, there are several options. Kitware provides a Debian/Ubuntu apt repository, as well as snap packages. There are universal Linux binaries provided, but you’ll need to pick an install location.
Kitware 提供了一个 Debian/Ubunutu apt 软件库,以及 snap 软件包。
Kitware APT Repository
https://apt.kitware.com/
This is Kitware, Inc.'s third-party APT repository, which we use for hosting our own Ubuntu packages, such as CMake.
Canonical Snapcraft
https://snapcraft.io/cmake
CMake is an open-source, cross-platform family of tools designed to build, test and package software.
There are universal Linux binaries provided, but you’ll need to pick an install location. If you already use ~/.local
for user-space packages, the following single line command will get CMake for you.
官方同时提供了 Linux 的二进制文件包,但需要你去选择一个安装位置。如果你已经使用 ~/.local
存放用户空间的软件包,下面的单行命令将为你安装 CMake。
$ wget -qO- "https://cmake.org/files/v3.31/cmake-3.31.0-linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C ~/.local
If you are in a critical environment, you should download the file and check the checksum.
如果你是在一个重要的环境中,你应该下载文件并检查校验码。
If you don’t have a .local
in your home directory, it’s easy to start. Just make the folder, then add export PATH="$HOME/.local/bin:$PATH"
to your .bashrc
or .bash_profile
or .profile
file in your home directory. Now you can install any packages you build to -DCMAKE_INSTALL_PREFIX=~/.local
instead of /usr/local
!
如果你的主目录中没有 .local
,想要开始也很容易。只要建立这个文件夹,然后把 export PATH="$HOME/.local/bin:$PATH"
添加到你的 .bashrc
or .bash_profile
or .profile
文件中。现在你可以把你构建的任何软件包安装到 -DCMAKE_INSTALL_PREFIX=~/.local
而不是 /usr/local
!
If you just want a local folder with CMake only:
$ mkdir -p cmake-3.31 && wget -qO- "https://cmake.org/files/v3.31/cmake-3.31.0-linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C cmake-3.31
$ export PATH=`pwd`/cmake-3.31/bin:$PATH
You’ll obviously want to append to the PATH
every time you start a new terminal, or add it to your .bashrc
or to an LMod
system.
显然,你要在每次启动新终端都追加一遍 PATH
,或将该指令添加到你的 .bashrc
或 LMod
系统中。
And, if you want a system install, install to /usr/local
; this is an excellent choice in a Docker container, for example on GitLab CI. Do not try it on a non-containerized system.
而且,如果你想进行系统安装,请安装到 /usr/local
;这在 Docker 容器中是一个很好的选择,例如在 GitLab CI 中。请不要在非容器化的系统上尝试。
docker $ wget -qO- "https://cmake.org/files/v3.31/cmake-3.31.0-linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C /usr/local
If you are on a system without wget
, replace wget -qO-
with curl -s
.
如果你在一个没有 wget
的系统上,请使用 curl -s
代替 wget -qO-
。
3.3. CMake Default Versions
3.3.1. Windows
The winget package is a good way to get CMake.
winget-pkgs/manifests/k/Kitware/CMake
https://github.com/microsoft/winget-pkgs/tree/master/manifests/k/Kitware/CMake
Other options:
https://community.chocolatey.org/packages/cmake
https://packages.msys2.org/base/mingw-w64-cmake
Also Scoop is generally up to date. The normal installers from CMake.org are common on Windows, too.
来自 CMake.org 的普通安装程序在 Windows 系统上通常也很常见。
ScoopInstaller/Main/bucket/cmake.json
https://github.com/ScoopInstaller/Main/blob/master/bucket/cmake.json
3.3.2. MacOS
Homebrew Formulae
https://formulae.brew.sh/formula/cmake
Homebrew Formulae
https://formulae.brew.sh/cask/cmake
MacPorts
https://ports.macports.org/port/cmake/details/
Homebrew is quite a bit more popular nowadays on macOS, at least according to Google Trends.
至少根据 Google Trends 的调查,如今 Homebrew 在 macOS 上的流行程度是相当高的。
3.3.3. Linux
3.3.3.1. RHEL/CentOS
https://rpms.remirepo.net/rpmphp/zoom.php?rpm=cmake
The default on 8 is not too bad, but you should not use the default on the end-of-life CentOS 7. Use the EPEL package instead.
CentOS 8 上的默认安装包不算太差,但最好不要使用 CentOS 7 上的默认安装包。请使用 EPEL 包来代替它。
3.3.3.2. Ubuntu
Ubuntu 18.04 package: cmake source package in Bionic
https://launchpad.net/ubuntu/bionic/+source/cmake
Ubuntu 20.04 package: cmake source package in Focal
https://launchpad.net/ubuntu/focal/+source/cmake
Ubuntu 22.04 package: cmake source package in Jammy
https://launchpad.net/ubuntu/jammy/+source/cmake
Ubuntu 24.04 package: cmake source package in Noble
https://launchpad.net/ubuntu/noble/+source/cmake
3.3.3.3. Debian
Versions for cmake
https://repology.org/project/cmake/versions
3.3.3.4. Other
Homebrew Formulae
https://formulae.brew.sh/formula/cmake
Versions for cmake
https://repology.org/project/cmake/versions
Alpine Linux
https://pkgs.alpinelinux.org/packages
3.3.4. General tools
Versions for cmake
https://repology.org/project/cmake/versions
pip install cmake
https://pypi.org/project/cmake/
anaconda / packages / cmake
https://anaconda.org/anaconda/cmake
Just pip install cmake
on many systems. Add --user
if you have to (modern pip
does this for you if needed). This does not supply Universal2 wheels yet.
在许多系统上只需 pip install cmake
。如果需要的话,请添加 --user
。
3.3.5. CI
If you are using GitHub Actions, also see the jwlawson/actions-setup-cmake action, which can install your selection of CMake, even in a docker action run.
如果你在使用 GitHub Actions,也可以查看 jwlawson/actions-setup-cmake 进行操作,它可以安装你选择的 CMake,即使是在 docker 中也可以操作运行。
3.3.6. Full list
Cmake Download for Linux (apk, deb, eopkg, pkg, rpm, tgz, txz, xbps, xz, zst)
https://pkgs.org/download/cmake
3.4. Quick list
- All
pip
, https://pypi.org/project/cmake/- anaconda / conda-forge, https://anaconda.org/anaconda/cmake / https://github.com/conda-forge/cmake-feedstock
- Windows
- winget-pkgs, https://github.com/microsoft/winget-pkgs/tree/master/manifests/k/Kitware/CMake
- Chocolatey, https://community.chocolatey.org/packages/cmake
- Scoop, https://github.com/ScoopInstaller/Main/blob/master/bucket/cmake.json
- MSYS2 Packages, https://packages.msys2.org/base/mingw-w64-cmake
- Kitware (official), https://cmake.org/download/
- MacOS
- Homebrew Formulae, https://formulae.brew.sh/formula/cmake
- MacPorts, https://ports.macports.org/port/cmake/details/
- Kitware (official), https://cmake.org/download/
- Linux
- Canonical Snapcraft (official), https://snapcraft.io/cmake
- Kitware APT Repository (official), https://apt.kitware.com/
- Kitware (official), https://cmake.org/download/
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/