Skip to content

Commit 4367bd5

Browse files
committed
通用Makefile
1 parent 933765e commit 4367bd5

File tree

15 files changed

+210
-3
lines changed

15 files changed

+210
-3
lines changed

Makefile/Makefil基础.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ b.o: b.c
1919
gcc -c -o b.o b.c
2020
```
2121

22-
***执行make命令,默认生成第一个目标,每次执行时,都检查是否需要重新生成依赖***
22+
***执行 make 命令时,它会去当前目录下查找名为“Makefile”的文件,并 根据它的指示去执行操作,生成第一个目标。每次执行时,都检查是否需要重新生成依赖***
2323

2424
***当文件很多时,不能逐一写出所有文件,而需要使用通配符%***
2525

@@ -252,7 +252,7 @@ clean:
252252

253253
- gcc -M c.c 打印出依赖
254254
- gcc -M -MF c.d c.c 把依赖文件写入c.d
255-
- gcc -c -o c.o c.c -MD -MF c.d 编译c.o,把依赖写入c.d
255+
- gcc -c -o c.o c.c -MD -MF c.d 编译c.c,把依赖写入c.d
256256

257257
```makefile
258258
objs = a.o b.o c.o
@@ -291,4 +291,3 @@ CFLAGS = -Werror -I include #CFLAGS是一个约定俗称的变量,当然可
291291
%.o : %.c
292292
gcc $(CFLAGS) -c -o $@ $< -MD -MF .$@.d
293293
```
294-

Makefile/Makefil基础.pdf

6.25 KB
Binary file not shown.
380 KB
Loading

Makefile/example/Makefile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
CROSS_COMPILE =
3+
AS = $(CROSS_COMPILE)as
4+
LD = $(CROSS_COMPILE)ld
5+
CC = $(CROSS_COMPILE)gcc
6+
CPP = $(CC) -E
7+
AR = $(CROSS_COMPILE)ar
8+
NM = $(CROSS_COMPILE)nm
9+
10+
STRIP = $(CROSS_COMPILE)strip
11+
OBJCOPY = $(CROSS_COMPILE)objcopy
12+
OBJDUMP = $(CROSS_COMPILE)objdump
13+
14+
export AS LD CC CPP AR NM
15+
export STRIP OBJCOPY OBJDUMP
16+
17+
CFLAGS := -Wall -O2 -g
18+
CFLAGS += -I $(shell pwd)/include
19+
20+
LDFLAGS :=
21+
22+
export CFLAGS LDFLAGS
23+
24+
TOPDIR := $(shell pwd)
25+
export TOPDIR
26+
27+
TARGET := test
28+
29+
30+
obj-y += main.o
31+
obj-y += sub.o
32+
obj-y += a/
33+
34+
35+
all : start_recursive_build $(TARGET)
36+
@echo $(TARGET) has been built!
37+
38+
start_recursive_build:
39+
make -C ./ -f $(TOPDIR)/Makefile.build
40+
41+
$(TARGET) : built-in.o
42+
$(CC) -o $(TARGET) built-in.o $(LDFLAGS)
43+
44+
clean:
45+
rm -f $(shell find -name "*.o")
46+
rm -f $(TARGET)
47+
48+
distclean:
49+
rm -f $(shell find -name "*.o")
50+
rm -f $(shell find -name "*.d")
51+
rm -f $(TARGET)
52+

Makefile/example/Makefile.build

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
PHONY := __build
2+
__build:
3+
4+
5+
obj-y :=
6+
subdir-y :=
7+
EXTRA_CFLAGS :=
8+
9+
include Makefile
10+
11+
# obj-y := a.o b.o c/ d/
12+
# $(filter %/, $(obj-y)) : c/ d/
13+
# __subdir-y : c d
14+
# subdir-y : c d
15+
__subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
16+
subdir-y += $(__subdir-y)
17+
18+
# c/built-in.o d/built-in.o
19+
subdir_objs := $(foreach f,$(subdir-y),$(f)/built-in.o)
20+
21+
# a.o b.o
22+
cur_objs := $(filter-out %/, $(obj-y))
23+
dep_files := $(foreach f,$(cur_objs),.$(f).d)
24+
dep_files := $(wildcard $(dep_files))
25+
26+
ifneq ($(dep_files),)
27+
include $(dep_files)
28+
endif
29+
30+
31+
PHONY += $(subdir-y)
32+
33+
34+
__build : $(subdir-y) built-in.o
35+
36+
$(subdir-y):
37+
make -C $@ -f $(TOPDIR)/Makefile.build
38+
39+
built-in.o : $(cur_objs) $(subdir_objs)
40+
$(LD) -r -o $@ $^
41+
42+
43+
44+
%.o : %.c
45+
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -Wp,-MD,$(dep_file) -c -o $@ $<
46+
47+
.PHONY : $(PHONY)

Makefile/example/a/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
EXTRA_CFLAGS := -D DEBUG
3+
CFLAGS_sub3.o := -D DEBUG_SUB3
4+
5+
obj-y += sub2.o
6+
obj-y += sub3.o

Makefile/example/a/sub2.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
#include <sub2.h>
3+
4+
void sub2_fun(void)
5+
{
6+
printf("Sub2 fun, B = %d!\n", B);
7+
#ifdef DEBUG
8+
printf("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
9+
#endif
10+
}

Makefile/example/a/sub3.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
#include <sub3.h>
3+
4+
void sub3_fun(void)
5+
{
6+
printf("Sub3 fun, C = %d!\n", C);
7+
8+
#ifdef DEBUG
9+
printf("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
10+
#endif
11+
12+
#ifdef DEBUG_SUB3
13+
printf("It is only debug info for sub3.\n");
14+
#endif
15+
16+
}

Makefile/example/include/sub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define A 1
2+
void sub_fun(void);

Makefile/example/include/sub2.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
#define B 2
3+
void sub2_fun(void);
4+

0 commit comments

Comments
 (0)