GNU Make Tips&Template
2009-11-20 15:21:32| 分类:
R&D
| 标签:
|举报
|字号大中小 订阅
Questions
如果源文件很多? =>引入规则
依赖关系很复杂? =>使用C预处理器挖掘依赖关系
#!/bin/sh
for i in *.c; do
cpp –M $i
done
Rules
目标(target)...: 依赖(prerequiries)…
<tab>命令(command)
1)隐含规则
自动推导
%.o %.c $(CC) –c $(CPPFLAGS) $(CFLAGS)
“-r”,“--no-builtin-rules”
2)模式规则
.c .o:
$(CC) $(CFLAGS) –c $<
%.o : %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
3)静态模式规则
<targets ...>: <target-pattern>: <prereq-patterns ...>
<commands>
$(OBJS): %.o:%.c
$(CC) -fpic $(CFLAGS) $(INCLUDE) -g -c $< -o $@ -DROOT_PATH=\"$(TOP)\"
Make的工作方式
1、读入所有的Makefile。
2、读入被include的其它Makefile。
3、初始化文件中的变量。
4、推导隐晦规则,并分析所有规则。
5、为所有的目标文件创建依赖关系链。
6、根据依赖关系,决定哪些目标要重新生成。
7、执行生成命令。
Make的参数
-n 或--just-print按实际执行是顺序显示命令,包括@开头的,但不实际执行
-p 显示makefile中所有宏定义和内部规则
-t 或--touch修改每个目标文件的创建日期,但不真正重新创建目标文件
Example
CC := gcc
CPP := cpp
SRCS := $(wildcard *.c)
OBJS := $(patsubst %.c,%.o,$(SRCS))
TARGET := editor
$(TARGET): $(OBJS)
$(CC) -o $@ $^
%.o: %.c
$(CC) -c $<
clean:
rm -f Depend *.o $(TARGET)
dep:
rm -f Depend
for i in $(SRCS); do \
$(CPP) -M $$i >> Depend; \
done
ifeq (Depend,$(wildcard Depend))
include Depend
endif
General complex makefile template
dir assumption:
system
- subsys_1
- subsys_2
- subsys_3
. module_a
. module_b
1).System level(top level) makefile
-----------------------------------
export TOP_DIR := $(shell pwd)
SUBDIRS := subsys_1 subsys_2 subsys_3
include $(TOP_DIR)/rules.make
2).Subsystem level makefile(subsys_1, subsys_2)
-----------------------------------------------
include $(TOP_DIR)/rules.make
3).Subsystem level makefile(subsys_3)
-------------------------------------
SUBDIRS := module_a module_b
include $(TOP_DIR)/rules.make
4).Module level makefile(module_a, module_b)
--------------------------------------------
include $(TOP_DIR)/rules.make
5).ruls.make
------------
CPP := cpp
CPPFLAGS := -M
CXX := g++
CXXFLAGS := -c -Wall
AS := as
ASFLAGS :=
LD := ld
LDFLAGS :=
PWD := $(shell pwd)
TARGET := $(PWD)/$(shell basename $(PWD)).target
SRCS := $(wildcard *.cpp *.s)
# or OBJS := $(subst .s,.o,$(subst .cpp,.o,$(SRCS)))
OBJS := $(patsubst %.cpp, %.o, $(patsubst %.s, %.o, $(SRCS)))
SUBTARGETS := $(foreach dir, $(SUBDIRS), $(dir)/$(dir).target)
.PHONY:$(SUBDIRS) dep clean touch
$(TARGET):$(OBJS) $(SUBDIRS)
$(LD) $(LDFLAGS) -o $@ $(OBJS) $(SUBTARGETS)
$(SUBDIRS):
make -C $@
%.o:%.cpp
$(CXX) $(CXXFLAGS) -o $@ $<
%.o:%.s
$(AS) $(ASFLAGS) -o $@ $<
dep:
@set -e
@rm -f Depend
@for i in $(SRCS); do $(CPP) $(CPPFLAGS) $$i >> Depend; done
@for i in $(SUBDIRS); do make -C $$i dep; done
clean:
@rm -f Depend *.o *.target
@for i in $(SUBDIRS); do make -C $$i clean ; done
ifeq (Depend, $(wildcard Depend))
include Depend
endif
评论这张
转发至微博
转发至微博
评论