Skip to content

Commit

Permalink
通用Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengshuo123 committed Mar 18, 2023
1 parent 933765e commit 4367bd5
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 3 deletions.
5 changes: 2 additions & 3 deletions Makefile/Makefil基础.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ b.o: b.c
gcc -c -o b.o b.c
```

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

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

Expand Down Expand Up @@ -252,7 +252,7 @@ clean:

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

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

Binary file modified Makefile/Makefil基础.pdf
Binary file not shown.
Binary file added Makefile/assets/image-20230318203906685.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions Makefile/example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

CROSS_COMPILE =
AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
CC = $(CROSS_COMPILE)gcc
CPP = $(CC) -E
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm

STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump

export AS LD CC CPP AR NM
export STRIP OBJCOPY OBJDUMP

CFLAGS := -Wall -O2 -g
CFLAGS += -I $(shell pwd)/include

LDFLAGS :=

export CFLAGS LDFLAGS

TOPDIR := $(shell pwd)
export TOPDIR

TARGET := test


obj-y += main.o
obj-y += sub.o
obj-y += a/


all : start_recursive_build $(TARGET)
@echo $(TARGET) has been built!

start_recursive_build:
make -C ./ -f $(TOPDIR)/Makefile.build

$(TARGET) : built-in.o
$(CC) -o $(TARGET) built-in.o $(LDFLAGS)

clean:
rm -f $(shell find -name "*.o")
rm -f $(TARGET)

distclean:
rm -f $(shell find -name "*.o")
rm -f $(shell find -name "*.d")
rm -f $(TARGET)

47 changes: 47 additions & 0 deletions Makefile/example/Makefile.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
PHONY := __build
__build:


obj-y :=
subdir-y :=
EXTRA_CFLAGS :=

include Makefile

# obj-y := a.o b.o c/ d/
# $(filter %/, $(obj-y)) : c/ d/
# __subdir-y : c d
# subdir-y : c d
__subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
subdir-y += $(__subdir-y)

# c/built-in.o d/built-in.o
subdir_objs := $(foreach f,$(subdir-y),$(f)/built-in.o)

# a.o b.o
cur_objs := $(filter-out %/, $(obj-y))
dep_files := $(foreach f,$(cur_objs),.$(f).d)
dep_files := $(wildcard $(dep_files))

ifneq ($(dep_files),)
include $(dep_files)
endif


PHONY += $(subdir-y)


__build : $(subdir-y) built-in.o

$(subdir-y):
make -C $@ -f $(TOPDIR)/Makefile.build

built-in.o : $(cur_objs) $(subdir_objs)
$(LD) -r -o $@ $^

dep_file = [email protected]

%.o : %.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -Wp,-MD,$(dep_file) -c -o $@ $<

.PHONY : $(PHONY)
6 changes: 6 additions & 0 deletions Makefile/example/a/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

EXTRA_CFLAGS := -D DEBUG
CFLAGS_sub3.o := -D DEBUG_SUB3

obj-y += sub2.o
obj-y += sub3.o
10 changes: 10 additions & 0 deletions Makefile/example/a/sub2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
#include <sub2.h>

void sub2_fun(void)
{
printf("Sub2 fun, B = %d!\n", B);
#ifdef DEBUG
printf("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
}
16 changes: 16 additions & 0 deletions Makefile/example/a/sub3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
#include <sub3.h>

void sub3_fun(void)
{
printf("Sub3 fun, C = %d!\n", C);

#ifdef DEBUG
printf("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif

#ifdef DEBUG_SUB3
printf("It is only debug info for sub3.\n");
#endif

}
2 changes: 2 additions & 0 deletions Makefile/example/include/sub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define A 1
void sub_fun(void);
4 changes: 4 additions & 0 deletions Makefile/example/include/sub2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#define B 2
void sub2_fun(void);

4 changes: 4 additions & 0 deletions Makefile/example/include/sub3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#define C 3
void sub3_fun(void);

14 changes: 14 additions & 0 deletions Makefile/example/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

extern void sub_fun(void);
extern void sub2_fun(void);
void sub3_fun(void);

int main(int argc, char* argv[])
{
printf("Main fun!\n");
sub_fun();
sub2_fun();
sub3_fun();
return 0;
}
7 changes: 7 additions & 0 deletions Makefile/example/sub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
#include "sub.h"

void sub_fun(void)
{
printf("Sub fun, A = %d!\n", A);
}
46 changes: 46 additions & 0 deletions Makefile/通用Makefle的解析.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
### 通用Makefle的解析

执行 make 命令时,它会去当前目录下查找名为“Makefile”的文件,并根据它的指示去执行操作,生成第一个目标。

- 我们可以使用“-f”选项指定文件,不再使用名为“Makefile”的文件, 比如:
```bash
make -f Makefile.build
```

- 我们可以使用“-C”选项指定目录,切换到其他目录里去,比如:
```bash
make -C a/ -f Makefile.build
```

- 我们可以指定目标,不再默认生成第一个目标:
```bash
make -C a/ -f Makefile.build other_target
```

- 变量的导出(export)

```bash
export CFLAGS LDFLAGS
#使其他Makefile能用到前面已经执行的Makefile的变量,即其他Makefile由导出变量的Makefile调用执行
```

- Makefile 中可以使用 shell 命令:
```makefile
TOPDIR := $(shell pwd)
#这是个立即变量,TOPDIR 等于 shell 命令 pwd 的结果。
```

- 在 Makefile 中怎么放置第 1 个目标:
执行 make 命令时如果不指定目标,那么它默认是去生成第 1 个目标。 所以“第 1 个目标”,位置很重要。有时候不太方便把第 1 个目标完整地放 在文件前面,这时可以在文件的前面直接放置目标,在后面再完善它的依赖与 命令。比如:

```bash
First_target: # 这句话放在前面
# 其他代码
First_target : $(xxx) #在后面来完善
$(command)
```

### 调用流程

![image-20230318203906685](assets/image-20230318203906685.png)

Binary file added Makefile/通用Makefle的解析.pdf
Binary file not shown.

0 comments on commit 4367bd5

Please sign in to comment.