一点一点学写Makefile(6)-遍历当前目录源文件及其子目录下源文件
2017-03-09 19:11
477 查看
时候,我们在开发的时候需要将本次工程的代码分成多个子目录来编写,但是在Makefile的编写上却是个问题,下面我就教大家怎么构建带有子文件夹的源代码目录的自动扫描编译
下面这张图是我的文件树
这里面src目录下是我的源代码,我将功能代码分成了三个子模块,分别为test1, test2, test3, 调用这三个子模块的是main.cpp文件,下面我将这三个子模块的代码
[cpp] view plain copy
// 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;
}
[cpp] view plain copy
// 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遍历的核心代码如下:
[plain] view plain copy
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的全部文件
下面这张图是我的文件树
这里面src目录下是我的源代码,我将功能代码分成了三个子模块,分别为test1, test2, test3, 调用这三个子模块的是main.cpp文件,下面我将这三个子模块的代码
[cpp] view plain copy
// 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;
}
[cpp] view plain copy
// 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遍历的核心代码如下:
[plain] view plain copy
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的全部文件
CROSS = CC = $(CROSS)gcc CXX = $(CROSS)g++ DEBUG = CFLAGS = $(DEBUG) -Wall -c MV = mv -f RM = rm -rf LN = ln -sf TARGET = classify TOP_PATH = $(shell pwd) SRC_PATH = $(TOP_PATH)/src MOD_PATH = $(TOP_PATH)/modules MOD_LIB_PATH = $(MOD_PATH)/lib MOD_INC_PATH = $(MOD_PATH)/include DIRS = $(shell find $(SRC_PATH) -maxdepth 3 -type d) FILES = $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp)) ########################################################## # modules ########################################################## modules = MODULES_PATH = $(foreach m, $(modules), $(MOD_PATH)/$(m)) ########################################################## # srcs ########################################################## 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 ########################################################## OBJS_CPP = $(patsubst %.cpp, %.o, $(SRCS_CPP)) OBJS_CC = $(patsubst %.cc, %.o, $(SRCS_CC)) OBJS_C = $(patsubst %.c, %.o, $(SRCS_C)) ########################################################## # paths ########################################################## INC_PATH += -I$(MOD_INC_PATH) INC_PATH += -I$(MOD_PATH) LIB_PATH += -L$(TOP_PATH)/lib LIB_PATH += -L$(MOD_LIB_PATH) ########################################################## # libs ########################################################## ########################################################## # building ########################################################## all:$(TARGET) $(TARGET) : $(OBJS_CPP) $(OBJS_CC) $(OBJS_C) @ for i in $(MODULES_PATH); \ do \ make -C $$i; \ done @ $(CXX) $^ -o $@ $(LIB_PATH) $(LIBS) @ echo Create $(TARGET) ok... $(OBJS_CPP):%.o : %.cpp $(CXX) $(CFLAGS) $< -o $@ $(INC_PATH) $(OBJS_CC):%.o : %.cc $(CXX) $(CFLAGS) $< -o $@ $(INC_PATH) $(OBJS_C):%.o : %.c $(CXX) $(CFLAGS) $< -o $@ $(INC_PATH) .PHONY : clean clean: @ $(RM) $(TARGET) $(OBJS_CPP) $(OBJS_CC) $(OBJS_C) @ for i in $(MODULES_PATH); \ do \ make clean -C $$i; \ done @echo clean all...
相关文章推荐
- 一点一点学写Makefile(6)-遍历当前目录源文件及其子目录下源文件
- 一点一点学写Makefile(6)-遍历当前目录源文件及其子目录下源文件
- 一点一点学写Makefile(2)-自动搜所当前目录下的所有源文件
- 一点一点学写Makefile(2)-自动搜所当前目录下的所有源文件
- 一点一点学写Makefile(2)-自动搜所当前目录下的所有源文件
- linux C 遍历目录及其子目录 opendir -> readdir -> closedir
- 对当前目录及子目录下的所有源码文件及Makefile打包
- linux C 遍历目录及其子目录 opendir -> readdir -> closedir
- 一份通用makefile,自动遍历子目录源文件,自动生成依赖。
- C# 趣味小程序(4)——遍历特定目录及其子目录
- shell 脚本实现的遍历目录修改子目录及其文件权限
- 使用Shell遍历目录及其子目录中的所有文件
- linux C 遍历目录及其子目录 opendir -> readdir -> closedir
- Makefile 递归遍历目录(含子目录) 编译动态库
- 遍历目录及其子目录,并输出后缀为jpg格式图片文件
- shell遍历目录及其子目录
- 续:C# 趣味小程序(4)——遍历特定目录及其子目录 (应用实例,压缩指定目录下的所有文件)
- 遍历当前目录及子目录,执行脚本
- Shell编程:在当前目录下遍历所有文件和子目录及子目录下的文件
- java与Groovy的互调用 实现在当前目录及其子目录删除指定名称的目录