Cmake

1.安装工具

1
2
3
4
5
6
7
8
9
10
11
12
#更新apt
sudo apt update
##安装必须工具 make,gcc与g++自带
# 安装cmake
sudo apt install cmake

#安装tree便于观察文件架构
sudo apt-get install tree
# 通过以下命令安装编译器和调试器
sudo apt install build-essential gdb
#make 一般都会预装,Ubuntu和Debian
sudo apt-get install make

验证工具是否安装

1
2
3
4
5
6
7
8
# 以下命令确认每个软件是否安装成功
# 如果成功,则显示版本号
gcc --version
g++ --version
gdb --version
make --version
cmake --version
tree --version

2.编写C++代码

  1. 创建项目文件夹

    1
    mkdir hello
  2. 在当前目录创建文件main.cpp

    1
    touch main.cpp
  3. 编写代码

    1
    2
    3
    4
    5
    6
    //main.cpp
    #include <stdio.h>
    int main(){
    printf("hello,world!");
    return 0;
    }

3.编写文件CmakeLists.txt

  1. 在当前目录创建文件CmakeLists.txt

    1
    touch CmakeLists.txt
  2. 编写内容

    1
    2
    3
    4
    5
    6
    7
    cmake_minimum_required(VERSION 3.10)

    #project name
    PROJECT(hello)

    #add executable file
    ADD_EXECUTABLE(hello main.c)
  3. 创建构建目录

    1
    2
    3
    4
    5
    6
    7
    8
       mkdir build

    ## 4.编译cmake项目

    4. 进入到构建目录

    ```sh
    cd build
  4. 使用cmake命令生成生成相关配置

    1
    2
    3
    4
    5
    6
       cmake ..

    6. 使用make命令编译程序

    ```sh
    make

5. 运行程序

1
./hello