本文讲述的是如何从 GitHub 上下载 CMake 源码并编译安装。
从GitHub上下载CMake源码
这里我们使用 git 直接克隆 GitHub 上的 CMake项目源码 到本地编译。
首先用 git 在本地创建了一个 mygithub 的本地仓库,然后将项目源码克隆到这个仓库下。
1. 搜索cmake项目
2. 获取源代码https链接
3. 克隆项目到本地仓库
命令:git clone https://github.com/Kitware/CMake.git
[root@localhost mygithub]# git clone https://github.com/Kitware/CMake.git
Cloning into 'CMake'...
remote: Enumerating objects: 382499, done.
remote: Counting objects: 100% (4125/4125), done.
remote: Compressing objects: 100% (2006/2006), done.
remote: Total 382499 (delta 2232), reused 3726 (delta 2046), pack-reused 378374
Receiving objects: 100% (382499/382499), 137.01 MiB | 64.00 KiB/s, done.
Resolving deltas: 100% (290672/290672), done.
Checking out files: 100% (22597/22597), done.
[root@localhost mygithub]#
进入 CMake 项目文件夹查看
[root@localhost mygithub]# ls -d CMake
CMake
[root@localhost mygithub]# cd CMake/
[root@localhost CMake]# ls
Auxiliary CMakeLists.txt CONTRIBUTING.rst doxygen.config README.rst
bootstrap CMakeLogo.gif Copyright.txt Help Source
CMakeCPack.cmake cmake_uninstall.cmake.in CTestConfig.cmake Licenses Templates
CMakeCPackOptions.cmake.in CompileFlags.cmake CTestCustom.cmake.in Modules Tests
CMakeGraphVizOptions.cmake configure DartConfig.cmake Packaging Utilities
[root@localhost CMake]#
4. 编译源代码
根据 CMake项目仓库 中的 README.rst 文件介绍编译命令如下:
Building CMake from Scratch
UNIX/Mac OSX/MinGW/MSYS/Cygwin
You need to have a C++ compiler (supporting C++11) and a make installed. Run the bootstrap script you find in the source directory of CMake. You can use the --help option to see the supported options. You may use the --prefix=<install_prefix> option to specify a custom installation directory for CMake. Once this has finished successfully, run make and make install.
For example, if you simply want to build and install CMake from source, you can build directly in the source tree:
mkdir cmake-build && cd cmake-build
$ ../cmake-source/bootstrap && make
由上可知,我们需要一个支持 C++11 的 C++ 编译器,并使用 ./bootstrap 、 make、 make install 这三个命令来编译和安装 cmake。
根据一开始黄色文字提到的,我们需要先安装好 autoconf、automake、libtool、gcc/g++(支持C++11) 这些依赖项。
-
执行 ./bootstrap
[root@localhost CMake]# ./bootstrap
---------------------------------------------
CMake 3.26.20230303, Copyright 2000-2023 Kitware, Inc. and Contributors
Found GNU toolchain
C compiler on this system is: gcc
C++ compiler on this system is: g++ -std=gnu++1y
Makefile processor on this system is: gmake
g++ has setenv
g++ has unsetenv
>>>>>>>>>>>>>> 省略部分内容显示 <<<<<<<<<<<<<<
---------------------------------------------
-- Configuring done (57.1s)
-- Generating done (1.1s)
-- Build files have been written to: /home/code/mygithub/CMake
---------------------------------------------
CMake has bootstrapped. Now run gmake.
[root@localhost CMake]#
此时查看,目录下生成了 Makefile 文件,接下来就可以进行编译和安装了。
- 执行 make 命令
[root@localhost CMake]# make
[ 0%] Building C object Source/kwsys/CMakeFiles/cmsys.dir/ProcessUNIX.c.o
[ 0%] Building C object Source/kwsys/CMakeFiles/cmsys.dir/Base64.c.o
>>>>>>>>>>>>>> 省略部分编译过程 <<<<<<<<<<<<<<
[100%] Linking C executable pseudo_cppcheck
[100%] Built target pseudo_cppcheck
[100%] Building CXX object Tests/FindPackageModeMakefileTest/CMakeFiles/foo.dir/foo.cpp.o
[100%] Linking CXX static library libfoo.a
[100%] Built target foo
[root@localhost CMake]#
到这里cmake已经编译成功了。接下来安装cmake。
- 执行 make install 命令
[root@localhost CMake]# make install
>>>>>>>>>>>>>> 省略部分安装过程 <<<<<<<<<<<<<<
-- Installing: /usr/local/share/emacs/site-lisp/cmake-mode.el
-- Installing: /usr/local/share/aclocal/cmake.m4
-- Installing: /usr/local/share/bash-completion/completions/cmake
-- Installing: /usr/local/share/bash-completion/completions/cpack
-- Installing: /usr/local/share/bash-completion/completions/ctest
[root@localhost CMake]#
- 查看 camke 安装的版本
新安装的 cmake 3.26 安装到了 /usr/local/ 路径下。
[root@localhost cmake]# cd /usr/local/bin/
[root@localhost bin]# ls
ccmake cmake cpack c_rehash ctest openssl re2c re2go re2rust
[root@localhost bin]# ./cmake --version
cmake version 3.26.20230303-gc465e0e
CMake suite maintained and supported by Kitware (kitware.com/cmake).
[root@localhost bin]#
- 生成cmake3软链接
[root@localhost cmake3.26]# ln -s /usr/local/bin/cmake /usr/bin/cmake3
[root@localhost cmake3.26]# cmake3 --version
cmake version 3.26.0-rc5
CMake suite maintained and supported by Kitware (kitware.com/cmake).
[root@localhost cmake3.26]#
有一次我编译的时候,由于没有安装 openssl 1.1.1 版本软件包,会导致编译出错。如果你也遇到了,可以先尝试安装 openssl 1.1.1 或更高版本后再试。
总结
编译安装使用的依赖软件包:git、autoconf、automake、libtool、make、openssl、C++编译器(支持C++11)等。