CMake 安装

CMake是一个跨平台的安装 编译 (Make)工具 ,可以通过简单的语句来描述安装过程。大部分情况下,我们的一些C或C++语言写的中间件都会用CMake来安装,比如安装 nginx、redis、memcah、mysql等。

Linux 安装 cmake

下载

wget https://github.com/Kitware/CMake/releases/download/v3.23.1/cmake-3.23.1-linux-x86_64.tar.gz

解压 ,进入cmake目录执行如下命令

./configure 或者 ./bootstrap

make && make install

查看版本

[root@node01 cmake-3.23.1]# cmake -version
cmake version 3.23.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

显示如上信息表示安装完成

Ubuntu 安装cmake

sudo apt-get update
sudo apt-get upgrade

sudo apt-get install g++ 
sudo apt-get install libssl-dev
sudo apt-get install make

其他步骤与Linux一样 下载、解压、 执行命令安装

CMake的简单示例

创建一个文件夹 /root/cmake

编写一个 hello.c

#include<stdio.h>
int main(){
  printf("hello test cmake\n");
  return 0;
}

编写 CMakeLists.txt

# 指定cmake最小版本
cmake_minimum_required(VERSION 3.24)
# 工程名字
PROJECT(CMAKE_HELLO)
# 设置变量
SET (SRC_LIST hello.c)
MESSAGE(STATUS "cmake version " ${CMAKE_MAJOR_VERSION})
MESSAGE(STATUS "BINARY dir " ${CMAKE_HELLO_BINARY_DIR})
MESSAGE(STATUS "SOURCE dir " ${CMAKE_HELLO_SOURCE_DIR})
# 生成可执行文件
ADD_EXECUTABLE(hello ${SRC_LIST})
# 提示 编译完后执行make
MESSAGE(STATUS "\n\n  ---please execute make \n")

执行 cmake .

[root@node01 cmake]# cmake .
CMake Error at CMakeLists.txt:2 (cmake_minimum_required):
  CMake 3.24 or higher is required.  You are running version 3.23.1


-- Configuring incomplete, errors occurred!

报错了,因为我们指定cmake版本最小要是3.24可是目前使用版本3.23.1

#修改 
#cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.23)
[root@node01 cmake]# cmake .
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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
-- cmake version 3
-- BINARY dir /root/cmake
-- SOURCE dir /root/cmake
-- 

  ---please execute make 

-- Configuring done
-- Generating done
-- Build files have been written to: /root/cmake

[root@node01 cmake]# ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  hello.c  Makefile

可以看到执行编译成功 当前目录多出了一些文件。接下来执行 make

[root@node01 cmake]# make
[ 50%] Building C object CMakeFiles/hello.dir/hello.c.o
[100%] Linking C executable hello
[100%] Built target hello

[root@node01 cmake]# ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  hello  hello.c  Makefile

多出来一个可执行文件hello,执行 hello 文件

[root@node01 cmake]# ./hello 
hello test cmake

可以看到打印出来的就是我们C代码的逻辑。

CMake的语法还是比较复杂,大家学习C的可以去了解下,我这里只是最简单的使用。许多中间件也是使用CMake来编译的,所以对于CMake的安装还是有必要知道的。


欢迎关注,学习不迷路!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值