千家信息网

Cmake怎么使用

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章主要讲解了"Cmake怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Cmake怎么使用"吧!1、多个源文件,使用命令 aux_sour
千家信息网最后更新 2026年10月31日Cmake怎么使用

这篇文章主要讲解了"Cmake怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Cmake怎么使用"吧!

1、多个源文件,使用命令 aux_source_directory(dir var):

在上一篇文章最后结尾的时候,有一个问题,就是在同一目录下面,有多个源文件的时候,这个时候你不能都往下面第三条命令里面一直手动添加源文件,那工作效率多低啊:

cmake_minimum_required(VERSION 2.8)

project(main)

add_executable(main main.c test1.c)

于是乎为了解决这种低效率的操作,在 cmake 里面有一条指令可以完全搞定这个问题;不过为了说明问题,在这之前我又添加了两个文件:test2.c 和 test2.h:

root@txp-virtual-machine:/home/txp/test# ls
1 cmake_install.cmake main.c test1.h touch2.c
CMakeCache.txt CMakeLists.txt Makefile test2.c touch2.h
CMakeFiles main test1.c test2.h

test2.c内容如下:

#include 
#include "test2.h"

void func1()
{
printf("i like the cmake\n");
}

test2.h内容如下:

#ifndef _TEST2_H_
#define _TEST2_H_

void func1();

#endif

最后main.c里面调用了func1函数:

#include 
#include "test1.h"
#include "test2.h"
int main(void)
{
func1();
func(8);
printf("TXP嵌入式\n");
return 0;

}

接下来我们的重点就来了,在cmake里面可以使用aux_source_directory(dir var)就可以搞定上面效率低的问题,接下来我们在CMakeLists.txt这样操作:

cmake_minimum_required(VERSION 2.8)

project(main)

aux_source_directory(. SRC_LIST)

add_executable(main ${SRC_LIST})


然后再进行编译:

root@txp-virtual-machine:/home/txp/test# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/txp/test


root@txp-virtual-machine:/home/txp/test# make
Scanning dependencies of target main
[ 25%] Building C object CMakeFiles/main.dir/main.c.o
[ 50%] Linking C executable main


root@txp-virtual-machine:/home/txp/test# ./main
i like the cmake
the b is 8
TXP嵌入式


说明:

aux_source_directory(. SRC_LIST):表示是把当当前目录下的所有源文件都添加到源列表变量里面去,最后用add_executable(main ${SRC_LIST})把所有有用的源文件加工成目标文件main。不过这方法也有他的缺点,就是把当前目录下的源文件都添加到变量SRC_LIST,如果我们不需要一些没有用的文件(只要拿到所需的源文件就行),可以进行这样操作:

cmake_minimum_required(VERSION 2.8)

project(main)

set(SRC_LIST
./main.c
./test1.c
./test2.c
)

add_executable(main ${SRC_LIST})

这样是能够通过编译的:

root@txp-virtual-machine:/home/txp/test# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/txp/test
root@txp-virtual-machine:/home/txp/test# make
[100%] Built target main

2、在上面的例子中,我们会发现同一目录下源文件比较乱,所以在cmake里面有这样的规则,可以把相同类型以及相关的源文件放到同一个目录,比如说,现在我在test目录下创建test1和test2两个目录文件,并同时把test1.c、test1.h、test2.c、test2.h分别放到这两个目录下去:

root@txp-virtual-machine:/home/txp/test# mkdir -p test1 test2
root@txp-virtual-machine:/home/txp/test# ls
@ CMakeFiles main test1 test2
1 cmake_install.cmake main.c test1.c test2.c
CMakeCache.txt CMakeLists.txt Makefile test1.h test2.h

然后把相关文件一到这两个目录文件下去:

root@txp-virtual-machine:/home/txp/test# mv test1.c test1.h test1

root@txp-virtual-machine:/home/txp/test# mv test2.c test2.h test2
root@txp-virtual-machine:/home/txp/test# ls
@ CMakeCache.txt cmake_install.cmake main Makefile test2
1 CMakeFiles CMakeLists.txt main.c test1

root@txp-virtual-machine:/home/txp/test# tree

├── cmake_install.cmake
├── CMakeLists.txt
├── main
├── main.c
├── Makefile
├── test1
│ ├── test1.c
│ └── test1.h
└── test2
├── test2.c
└── test2.h


然后这个时候要修改CMakeLists.txt里面的规则属性了:

cmake_minimum_required(VERSION 2.8)

project(main)

include_directories(test1 test2)
aux_source_directory(test1 SRC_LIST)
aux_source_directory(test2 SRC_LIST1)

add_executable(main main.c ${SRC_LIST} ${SRC_LIST1})

然后编译输出,也是能够通过的:

root@txp-virtual-machine:/home/txp/test# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/txp/test
root@txp-virtual-machine:/home/txp/test# make
Scanning dependencies of target main
[ 25%] Building C object CMakeFiles/main.dir/main.c.o
[ 50%] Building C object CMakeFiles/main.dir/test1/test1.c.o
[ 75%] Building C object CMakeFiles/main.dir/test2/test2.c.o
[100%] Linking C executable main
[100%] Built target main
root@txp-virtual-machine:/home/txp/test# ls
@ CMakeCache.txt cmake_install.cmake main Makefile test2
1 CMakeFiles CMakeLists.txt main.c test1
root@txp-virtual-machine:/home/txp/test# ./main
i like the cmake
the b is 8
TXP嵌入式

感谢各位的阅读,以上就是"Cmake怎么使用"的内容了,经过本文的学习后,相信大家对Cmake怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

源文件 目录 文件 问题 两个 内容 时候 就是 嵌入式 学习 编译 接下来 变量 命令 多个 效率 有用 篇文章 规则 相同 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网最新的科技 互联网科技视频 互联网科技论文 科技互联网创业计划书 重庆科技学院互联网 科技大学互联网专业好不好 互联网科技公司招聘 开启互联网科技时代小说 团飞科技与伯堂互联网服务平台 互联网系的金融科技公司 互联网金融科技公司干啥的 江苏苏星互联网科技有限公司 21世纪互联网科技成就 富国互联网科技002692 有人说阿里系是互联网科技企业 易企秀互联网科技是做什么的 上海同邻互联网科技有限公司规模 互联网科技平台产品运营 数字互联网科技有限公司 杭州融税互联网科技有限公司 互联网科技 互联网 的科技创新 奇酷互联网络科技 互联网金融科技平台 银川新华互联网科技职业技术培训学校图片 栖莱科技互联网股份有限公司 互联网为啥属于高科技 元气玛特互联网科技公司是外包吗 中国新注册互联网科技 众联互联网络科技
0