时候,我们在开发的时候需要将本次工程的代码分成多个子目录来编写,但是在Makefile的编写上却是个问题,下面我就教大家怎么构建带有子文件夹的源代码目录的自动扫描编译

下面这张图是我的文件树

这里面src目录下是我的源代码,我将功能代码分成了三个子模块,分别为test1, test2, test3, 调用这三个子模块的是main.cpp文件,下面我将这三个子模块的代码

  1. //  src/test1/test1.h
  2. #ifndef __TEST1_H__
  3. #define __TEST1_H__
  4. int test1();
  5. #endif //__TEST1_H__
  6. // src/test1/test1.cpp
  7. #include "test1.h"
  8. int test1() {
  9. return 1;
  10. }
  11. <pre name="code" class="cpp">//  src/test2/test2.h
  12. #ifndef __TEST2_H__
  13. #define __TEST2_H__
  14. int test2();
  15. #endif //__TEST2_H__
  16. // src/test2/test2.cpp
  17. #include "test2.h"
  18. int test2() {
  19. return 2;
  20. }
  1. //  src/test3/test3.h
  2. #ifndef __TEST3_H__
  3. #define __TEST3_H__
  4. int test3();
  5. #endif //__TEST3_H__
  6. // src/test3/test3.cpp
  7. #include "test3.h"
  8. int test3() {
  9. return 3;
  10. }

// 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遍历的核心代码如下:

  1. SRC_PATH = ./src
  2. DIRS = $(shell find $(SRC_PATH) -maxdepth 3 -type d)
  3. # 为了更大幅度的支持项目的搭建,将三种文件格式的后缀都单独便利到变量中
  4. SRCS_CPP += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp))
  5. SRCS_CC += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cc))
  6. SRCS_C += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c))
  7. OBJS_CPP = $(patsubst %.cpp, %.o, $(SRCS_CPP))
  8. OBJS_CC = $(patsubst %.cc, %.o, $(SRCS_CC))
  9. OBJS_C = $(patsubst %.c, %.o, $(SRCS_C))

下面是Makefile的全部文件

最新文章

  1. Duilib源码分析(三)XML解析器—CMarkup
  2. Android仿微信二维码扫描
  3. ASP.NET MVC4 请不要将你的Control命名为APIController
  4. 钉钉如何进行PC端开发
  5. 1110Nested Loop Join算法
  6. Windows 服务器使用FTP出现“当前的安全设置不允许从该位置下载文件&quot; 警告
  7. Balanced Lineup 倍增思想到ST表RMQ
  8. github的large file storeage
  9. LINUX Find命令使用
  10. java正则表达式应用--验证字符串是否为数字(转载)
  11. asp.net能不托管吗?
  12. PHPCMSv9 更改后台地址(测试)
  13. 循环之while
  14. JAVA序列化在IO中读写对象的使用
  15. C#之不安全代码
  16. IIS下uploadify上传大文件出现404错误(提示上传文件大小超过400M)
  17. 17-vue-cli脚手架安装和webpack-simple模板项目生成
  18. POJ 1845 Sumdiv(逆元)
  19. 数组B:我想我需要一艘船屋
  20. 在deepin中安装docker

热门文章

  1. PHPCMS中load_model,load_app_class, load_sys_func
  2. Win32 进程间通信的分析与比较
  3. c语言3种方式实现参数传递
  4. web安全——文件上传
  5. Python+Selenium定位元素的方法
  6. layer框架使用的问题汇总
  7. Enjoy coding
  8. HDU 1166——敌兵布阵——————【线段树单点增减、区间求和】
  9. 【Linux】网络性能测试工具iperf详细使用图文教程【转】
  10. mvc中RedirectToAction()如何传参?