-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
933765e
commit 4367bd5
Showing
15 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#define A 1 | ||
void sub_fun(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
#define B 2 | ||
void sub2_fun(void); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
#define C 3 | ||
void sub3_fun(void); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` | ||
|
||
### 调用流程 | ||
|
||
 | ||
|
Binary file not shown.