CMake在BC-Linux服务器上源码编译和安装

本文讲述的是如何从GitHub上下载 CMake 源码并编译安装。
提示:一旦涉及编译源码安装,你需要安装的基本的依赖项有:git、autoconf、automake、libtool、make、cmake

从GitHub上下载CMake源码

这里我们使用 git 直接克隆 GitHub 上的 CMake项目源码 到本地编译。
首先用 git 在本地创建了一个 mygithub 的本地仓库,然后将项目源码克隆到这个仓库下。

1. 搜索cmake项目

搜索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:
$ ./bootstrap && make && sudo make install
Or, if you plan to develop CMake or otherwise run the test suite, create a separate build tree:
$ mkdir cmake-build && cd cmake-build
$ …/cmake-source/bootstrap && make

由上可知,我们需要一个支持 C++11C++ 编译器,并使用 ./bootstrapmakemake 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)等


好了,cmake 的安装分享完毕,希望帮到你,谢谢阅览。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
cmake-3.15.2-linux-x86_64.tar.gz是一个CMake的压缩文件,适用于Linux系统的x86_64架构。CMake是一个跨平台的开源工具,用于自动构建和管理软件项目。通过使用CMake,开发人员可以编写一个CMakeLists.txt文件来描述项目的构建过程,然后使用CMake工具来生成项目所需的Makefile文件。 cmake-3.15.2-linux-x86_64.tar.gz是CMake 3.15.2版本的二进制发布包,在Linux系统上可以直接使用。用户可以通过下载该压缩文件并解压缩,来获取CMake的可执行文件和相关的库文件。 解压后,用户可以将CMake可执行文件所在的目录添加到系统的环境变量中,以便在任意位置可以直接运行CMake命令。使用CMake命令行工具,开发人员可以在项目的源代码目录下执行cmake命令,根据CMakeLists.txt文件来生成项目所需的Makefile文件。然后,可以使用生成的Makefile文件来编译、构建和安装项目。 CMake还支持许多功能和选项,例如支持不同的构建类型(Release、Debug等)、自定义构建规则、选择特定的编译器、链接库和头文件、自动查找依赖项等。通过CMake,可以实现跨平台的构建和部署,减少了在不同操作系统和编译环境下进行配置和构建的繁琐步骤。 总而言之,cmake-3.15.2-linux-x86_64.tar.gz是CMake工具在Linux系统x86_64架构下的一个特定版本的发布包,可以通过解压缩来获取CMake工具的可执行文件和库文件,从而方便地进行项目的构建和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫叶2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值