微网站介绍,合肥小程序建设,wordpress 拍卖,网站建设的开发的主要方法u-boot#xff08;2014.04#xff09;是通过顶层makefile调用各子目录中的makefile来实现整个工程的编译的#xff0c;实际上子目录的makefile是include进来的。这里仿照这种结构写个模板测试一下。 目录结构#xff1a; mytest#xff1a; add#xff1a; mul#xff1…u-boot2014.04是通过顶层makefile调用各子目录中的makefile来实现整个工程的编译的实际上子目录的makefile是include进来的。这里仿照这种结构写个模板测试一下。 目录结构 mytest add mul Makefile mul.c mul.h add.c add.h Makefile main main.c Makefile scripts Makefile.build Makefile.clean Makefile.lib sub div div.c div.h Makefile sub.c sub.h Makefile 顶层Makefile 1 CC : gcc2 LD : ld3 4 CFLAGS : -g5 6 PHONY : all7 8 target : mainApp9
10 # default target
11 all: $(target)
12
13 # dirs to be compiled
14 src-dirs add/
15 src-dirs sub/
16 src-dirs main/
17
18 # libs to be linked
19 libs-y : $(patsubst %/,%/built-in.o,$(src-dirs))
20
21 src-dirs : $(patsubst %/,%,$(src-dirs))
22
23 # include dirs
24 inc-dirs : $(addprefix -I,$(src-dirs))
25
26 # the Makefile for building
27 build : -f scripts/Makefile.build obj
28
29 CFLAGS $(inc-dirs)
30 export CC LD CFLAGS
31
32
33 PHONY $(src-dirs)
34
35 # linking
36 $(target): $(src-dirs)
37 $(CC) -o $ $(libs-y)
38
39 # compiling source dirs
40 $(src-dirs):
41 make $(build)$
42
43 # Clean
44 clean-dirs : $(foreach f,$(src-dirs),$(if $(wildcard $f/Makefile),$f))
45 clean-dirs : $(addprefix _clean_, $(clean-dirs))
46 PHONY clean $(clean-dirs)
47
48 # descending to subdirs for cleaning
49 $(clean-dirs):
50 make $(clean)$(patsubst _clean_%,%,$)
51
52 # just simply remove .o and target
53 clean: $(clean-dirs)
54 find . \( -name *.o -o -name $(target) \) -type f | xargs rm -f
55
56 clean : -f scripts/Makefile.clean obj
57 .PHONY : $(PHONY) 顶层Makefile确定target要编译的源码目录include的路径。 第41行使用scripts/Makefile.build对各个源码目录进行编译Makefile.build为实际编译代码的makefile内容如下 1 src : $(obj)2 3 PHONY : __build4 5 obj-y : 6 7 # include the Makefile in the $(obj) dir8 build-dir : $(src)9 build-file : $(build-dir)/Makefile
10 include $(build-file)
11
12 # include scripts/Makefile.lib
13 include scripts/Makefile.lib
14
15 build : -f scripts/Makefile.build obj
16
17 ifneq ($(strip $(obj-y)),)
18 builtin-target : $(obj)/built-in.o
19 endif
20
21 # default target
22 __build: $(builtin-target) $(subdir)
23 :
24
25 # compiling .c files
26 $(obj)/%.o: $(src)/%.c
27 $(CC) -c $(CFLAGS) $ -o $
28
29 # subdir object
30 $(subdir-obj-y): $(subdir) ;
31
32 # linking objs
33 $(builtin-target): $(obj-y)
34 $(LD) -r -o $ $^
35
36 PHONY $(subdir)
37
38 # descending to subdirs
39 $(subdir):
40 make $(build)$
41
42 .PHONY : $(PHONY) obj变量为需要编译的源码目录例如add第10行将add/目录下的Makefile目录包含进来add/Makefile内容如下 1 obj-y add.o
2 obj-y mul/ 这里仅仅确定需编译的文件以及子目录回到makefile.build第13行包含scripts/Makefile.lib文件内容如下 1 subdir : $(patsubst %/,%,$(filter %/, $(obj-y)))
2 obj-y : $(patsubst %/,%/built-in.o, $(obj-y))
3 subdir-obj-y : $(filter %/built-in.o, $(obj-y))
4
5 subdir : $(addprefix $(obj)/,$(subdir))
6 obj-y : $(addprefix $(obj)/,$(obj-y))
7 subdir-obj-y : $(addprefix $(obj)/, $(subdir-obj-y)) obj-y表示要编译的目标文件如果是目录如mul/则表示mul/built-in.osubdir表示要继续编译的子目录subdir-obj-y表示子目录下的built-in.o 回到makefile.build第18行每个要编译的源码目录编译之后都将会产生一个built-in.o的文件第27行对.c文件进行编译即由obj-y变量所确定的文件。第40行对子目录执行同样的过程。第33行对每个目录链接出一个built-in.o文件这个built-in.o由当前目录下.o文件和子目录下的.built-in.o文件链接而成。 在所有的源码目录都经过编译之后回到顶层makefile执行最后的链接得到target。 再看看clean目标第49行首先在需要进行clean的目录下使用scripts/Makefile.clean执行clean需要clean的目录为含有makefile的源码目录。Makefile.clean内容如下 1 src : $(obj)2 3 PHONY : __clean4 5 # default target6 __clean:7 8 clean : -f scripts/Makefile.clean obj9
10 # include the Makefile in the $(obj) dir
11 build-dir : $(src)
12 build-file : $(build-dir)/Makefile
13 include $(build-file)
14
15 # subdir to be cleaned
16 subdir : $(patsubst %/,%,$(filter %/,$(obj-y)))
17 subdir : $(addprefix $(obj)/,$(subdir))
18
19 __clean-files :
20 __clean-files : $(addprefix $(obj),$(__clean-files))
21
22 __clean-dirs :
23 __clean-dirs : $(addprefix $(obj),$(__clean-dirs))
24
25 __clean: $(subdir)
26 ifneq ($(strip $(__clean-files)),)
27 rm -f $(__clean-files)
28 endif
29
30 ifneq ($(strip $(__clean-dirs)),)
31 rm -rf $(__clean-dirs)
32 endif
33
34 PHONY $(subdir)
35 $(subdir):
36 make $(clean)$
37
38 .PHONY: $(PHONY) 这里也是一个逐步往下clean的过程要clean的文件由__clean-files变量指定。 回到顶层makefile简单的一个find操作删除目录下所有.o文件和目标。 关于include路径u-boot讲所有头文件都集中放在了某几个目录下于是源码中要包含其他头文件的时候是这样子做的xxx/xxx/xxx.h我这里的main.c如下其中对与mul和div的包含也是类似。 1 #include stdio.h2 #include add.h3 #include sub.h4 #include mul/mul.h5 #include div/div.h6 7 void main(void)8 {9 int a 1;
10 int b 2;
11 printf(a %d, b %d\n, a, b);
12 printf(a b %d\n, add(a, b));
13 printf(a - b %d\n, sub(a, b));
14 printf(a * b %d\n, mul(a, b));
15 printf(a / b %d\n, div(a, b));
16 } u-boot的makefile对我来说还是太复杂了这里仅仅是它的冰山一角可是我已经吃不消了。。。转载于:https://www.cnblogs.com/heyxiaotang/p/6438162.html