一点一点学写Makefile(6)-遍历当前目录源文件及其子目录下源文件
2024-08-27 08:01:02
时候,我们在开发的时候需要将本次工程的代码分成多个子目录来编写,但是在Makefile的编写上却是个问题,下面我就教大家怎么构建带有子文件夹的源代码目录的自动扫描编译
下面这张图是我的文件树
这里面src目录下是我的源代码,我将功能代码分成了三个子模块,分别为test1, test2, test3, 调用这三个子模块的是main.cpp文件,下面我将这三个子模块的代码
- // src/test1/test1.h
- #ifndef __TEST1_H__
- #define __TEST1_H__
- int test1();
- #endif //__TEST1_H__
- // src/test1/test1.cpp
- #include "test1.h"
- int test1() {
- return 1;
- }
- <pre name="code" class="cpp">// src/test2/test2.h
- #ifndef __TEST2_H__
- #define __TEST2_H__
- int test2();
- #endif //__TEST2_H__
- // src/test2/test2.cpp
- #include "test2.h"
- int test2() {
- return 2;
- }
- // src/test3/test3.h
- #ifndef __TEST3_H__
- #define __TEST3_H__
- int test3();
- #endif //__TEST3_H__
- // src/test3/test3.cpp
- #include "test3.h"
- int test3() {
- return 3;
- }
// src/main.cpp
#include <iostream>
#include "test1/test1.h"
#include "test2/test2.h"
#include "test3/test3.h"
using namespace std;
int main() {
cout << "test1()" << test1() << endl;
cout << "test2()" << test2() << endl;
cout << "test3()" << test3() << endl;
}
Makefile遍历的核心代码如下:
- SRC_PATH = ./src
- DIRS = $(shell find $(SRC_PATH) -maxdepth 3 -type d)
- # 为了更大幅度的支持项目的搭建,将三种文件格式的后缀都单独便利到变量中
- SRCS_CPP += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp))
- SRCS_CC += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cc))
- SRCS_C += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c))
- OBJS_CPP = $(patsubst %.cpp, %.o, $(SRCS_CPP))
- OBJS_CC = $(patsubst %.cc, %.o, $(SRCS_CC))
- OBJS_C = $(patsubst %.c, %.o, $(SRCS_C))
下面是Makefile的全部文件
最新文章
- Duilib源码分析(三)XML解析器—CMarkup
- Android仿微信二维码扫描
- ASP.NET MVC4 请不要将你的Control命名为APIController
- 钉钉如何进行PC端开发
- 1110Nested Loop Join算法
- Windows 服务器使用FTP出现“当前的安全设置不允许从该位置下载文件"; 警告
- Balanced Lineup 倍增思想到ST表RMQ
- github的large file storeage
- LINUX Find命令使用
- java正则表达式应用--验证字符串是否为数字(转载)
- asp.net能不托管吗?
- PHPCMSv9 更改后台地址(测试)
- 循环之while
- JAVA序列化在IO中读写对象的使用
- C#之不安全代码
- IIS下uploadify上传大文件出现404错误(提示上传文件大小超过400M)
- 17-vue-cli脚手架安装和webpack-simple模板项目生成
- POJ 1845 Sumdiv(逆元)
- 数组B:我想我需要一艘船屋
- 在deepin中安装docker