CMake 常见的安装方式有两种:
- apt 安装
- 源码安装
- 二进制包直接安装
apt方式
安装
直接sudo apt install cmake
这样安装的 cmake 多半不是最新版.
推荐方式:
参照官网, 添加cmake 自己的源之后 再cmake 安装就是最新版了.
添加cmake 官方的key
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null
添加主仓库
对于 Ubuntu Focal Fossa (20.04):
sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ focal main'
sudo apt-get update
对于 Ubuntu Bionic Beaver (18.04):
sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main'
sudo apt-get update
对于 Ubuntu Xenial Xerus (16.04):
sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ xenial main'
sudo apt-get update
添加候选版本仓库(可选)
对于Ubuntu Focal Fossa (20.04):
sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ focal-rc main'
sudo apt-get update
对于 Ubuntu Bionic Beaver (18.04):
sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic-rc main'
sudo apt-get update
对于 Ubuntu Xenial Xerus (16.04):
sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ xenial-rc main'
sudo apt-get update
自动更新仓库的key(可选,推荐!!)
sudo apt-get install kitware-archive-keyring
sudo rm /etc/apt/trusted.gpg.d/kitware.gpg
安装cmake
sudo apt install cmake
卸载
其实这个没啥好说的,就是常规的apt卸载方式.
sudo apt remove cmake
或者sudo apt purge cmake
,前者保留cmake配置文件,后者是卸个干净.
源码方式
安装
去官网下载对应平台的源码,使用tar xvf cmake-xxx.tar.gz
解压.然后进入源码目录,执行:
./bootstrap
make
make install
./bootstrap
可能提示报错:
Error when bootstrapping CMake:
Cannot find a C++ compiler that supports both C++11 and the specified C++ flags.
Please specify one using environment variable CXX.
…
这多半是 g++ 版本过低不支持 C++ 11,执行sudo apt-get install g++ gcc
更新一下即可.
make
的时候可能报错提示缺少某些依赖,make clean
之后,使用 apt
装好即可.常见缺少的依赖:
sudo apt-get install libssl-dev
卸载
参见官方wiki,最简单做法是在源码目录下执行:
sudo xargs rm < install_manifest.txt
不建议直接使用make uninstall
卸载.
可能有的老哥说我源码删了怎么办,这里放一份cmake-3.19的 install_manifest.txt 包含的大致内容表供参考,系统是 Ubuntu 18.04.
/usr/local/doc/cmake-3.19/
/usr/local/bin/ccmake
/usr/local/bin/cmake
/usr/local/bin/ctest
/usr/local/bin/cpack
/usr/local/share/cmake-3.19/
/usr/local/share/vim/vimfiles/indent/cmake.vim
/usr/local/share/vim/vimfiles/syntax/cmake.vim
/usr/local/share/emacs/site-lisp/cmake-mode.el
/usr/local/share/aclocal/cmake.m4
/usr/local/share/bash-completion/completions/cmake
/usr/local/share/bash-completion/completions/cpack
/usr/local/share/bash-completion/completions/ctest
二进制包方式
安装
直接下载的二进制包是编译好,可以直接运行的.
去官网下载页面,在 Binary distributions 一栏选择对于平台,下载tar包之后解压, 以 20 版本为例,解压之后:
mv cmake-3.20.0-rc3-linux-x86_64 /opt/cmake-3.20.0-rc3
ln -sf /opt/cmake-3.20.0-rc3/bin/* /usr/bin/
卸载
直接删掉上面的文件和软链接即可
参考
- https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#does-cmakes-make-install-support-destdir
- https://apt.kitware.com/
- https://m.linuxidc.com/Linux/2018-09/154165.htm