一、ubuntu中安装cmake
sudo apt-get install cmake
![在这里插入图片描述](https://img-blog.csdnimg.cn/25a1232849834ec599878c03eba03e04.png)
cmake --version
![在这里插入图片描述](https://img-blog.csdnimg.cn/e945fb2669d3414b81b2b59bec9bd78c.png)
二、单个源文件
main.c
#include<stdio.h>
int main()
{
printf("this is my first cmake test\n");
return 0;
}
CMakeLists.txt的编写
- 不能是cmakelist.txt,应该是CMakeLists.txt
![在这里插入图片描述](https://img-blog.csdnimg.cn/583f7a5dd05a4a589b8cf751665f8785.png)
mv cmakelists.txt CMakeLists.txt
![在这里插入图片描述](https://img-blog.csdnimg.cn/c3f190ed27d2468db76907ad34f92b4e.png)
cmake .
![在这里插入图片描述](https://img-blog.csdnimg.cn/da97b92c4fcd4d2e9ec51b8582382728.png)
make
![在这里插入图片描述](https://img-blog.csdnimg.cn/0348c7503229492c8bedd8249ac4a6be.png)
- 执行编译生成的可执行文件
![在这里插入图片描述](https://img-blog.csdnimg.cn/71280e3ed8e54ef59e8dc4d332208556.png)
三、多个源文件
main.c
#include"test1.h"
int main()
{
printf("this is my first cmake test\n");
print_test_1();
return 0;
}
test1.c
#include "test1.h"
void print_test_1()
{
printf("this is in print_test_1 function\n");
}
test1.h
#ifndef TEST_H_
#define TEST_H_
#include <stdio.h>
void print_test_1();
#endif
CMakeLists.txt的编写
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (demo1)
# 指定生成目标
add_executable(Demo main.c test1.c)
cmake .
make
![在这里插入图片描述](https://img-blog.csdnimg.cn/44fb7134e00745639ef9cd39d16d04fa.png)