原文转自:http://blog.sina.com.cn/s/blog_87c063060101c9yp.html
1、在写 多目录下makefile的时候,碰到一个错误提示,让我纠结许久,后面还是解决了,这个错误不容易被发现。
2、错误提示如下:
Makefile:8: *** missing separator (did you mean TAB instead of 8 spaces?). Stop.
3、先来说一下我想干什么,有些什么?
想干什么: 搭建一个Linux LCD的裸机开发环境,注意是多目录下面的,想写 一个比较通用的makefile。
有什么:父目录①←子目录①(build目录)、子目录①(code目录)
子目录①(build目录)←Makefile make.rule
子目录①(code目录)← 子目录②(startup)、子目录②(lib)、子目录②(foo)、子目录②(huge)
如下图:
1.工程目录下:
2.build 目录下
3.code目录下
4.code目录下的子目录都有2个子目录: src目录(存放.c文件) 、 include目录(存放.h文件)
5、下面贴一下makefile的内容
a. build下面的make.rule:
.PHONY:all clean
CC = arm-linux-gcc
LD = arm-linux-ld
AR = arm-linux-ar
ARFLAGS = crs
OBJCOPY = arm-linux-objcopy
OBJDUMP = arm-linux-objdump
CFLAGS := -Wall -O2
RM = rm
RMFLAGS = -fr
export CC LD AR RM RMFLAGS ARFLAGS OBJCOPY OBJDUMP CFLAGS
SRCS = $(wildcard *.c)
ASS = $(wildcard *.S)
OBJS := $(SRCS:.c=.o)
ASMS := $(ASS:.S=.o )
DEPS := $(SRCS:.c=.dep)
ifneq ("$(EXE)","")
EXE := $(EXE)
RMS += $(EXE)
endif
ifneq ("$(LIB)","")
LIB := $(LIB)
RMS += $(LIB)
endif
ifneq ($(INCLUDE_DIRS), "")
INCLUDE_DIRS := $(strip $(INCLUDE_DIRS))
INCLUDE_DIRS := $(addprefix -I, $(INCLUDE_DIRS))
endif
ifneq ($(LINK_LIBS), "")
LINK_LIBS := $(strip $(LINK_LIBS))
LIB_ALL := $(notdir $(wildcard $(DIR_LIB)/*))
LIB_FILTERED := $(addsffix %,$(addprefix lib, $(LINK_LIBS)))
$(evel DEP_LIBS = $(filter $(LIB_FILTERED), $(LIB_ALL) ) )
DEP_LIBS := $(addprefix $(DIR_LIBS)/, $(DEP_LIBS))
LINK_LIBS := $(addprefix -l, $(LINK_LIBS))
endif
all: $(EXE) $(LIB)
$(EXE):$(OBJS) $(LIB)
$(CC) $(CFLAGS) $(INCLUDE_DIRS) -o $@ $^
$(LIB):$(ASMS) $(OBJS)
$(AR) -$(ARFLAGS) $@ $^
%.o : %.c
$(CC) $(CFLAGS) $(INCLUDE_DIRS) -o $@ -c $<</P>
%.o : %.S
$(CC) $(CFLAGS) $(INCLUDE_DIRS) -o $@ -c $<</P>
%.dep: %.c
@echo "Creating $@ ..."
@set -e;\
$(RM) $(RMFLAGS) [email protected] ;\
$(CC) $(INCLUDE_DIRS) -E -MM $(filter %.c, $^) > [email protected] ; \
sed 's,\(. *\)\.o[ :]*,objs/\1.o $@: ,g' < [email protected] > $@ ; \
$(RM) $(RMFLAGS) [email protected]
clean:
$(RM) $(RMFLAGS) *.bin *.bak *.o *.dep *.a
b. build下面的 Makefile:
.PHONY: all clean touch
ROOT = $(realpath ..)
#注意:因为 huge目录下 是编译 可执行文件(最终目标).所以对于 foo bar huge 这三个目录,huge 需要放到 最后编译。
DIRS = $(ROOT)/code/startup/src \
$(ROOT)/code/lib/src \
$(ROOT)/code/foo/src \
$(ROOT)/code/huge/src
RM = rm
RMFLAGS = -fr
RMS = $(ROOT)/build/exes $(ROOT)/build/libs
all clean:
@set -e; \
for dir in $(DIRS); \
do \
cd $$dir && $(MAKE) ROOT=$(ROOT) $@; \
done
@set -e;\
if ["$(MAKECMDGOALS)" = "clean" ]; \
then $(RM) $(RMFLAGS) $(RMS); fi
@echo ""
@echo ":-) Completed"
@echo ""
touch:
@echo "Processing ......"
@find $(ROOT) -exec touch {} \;
@echo ""
@echo ":-) Completed"
@echo ""
c.huge/src 下面的 makefile:
正确的写法如下:
EXE = huge.bin
LIB =
INCLUDE_DIRS = $(ROOT)/code/huge/include \
$(ROOT)/code/startup/include \
$(ROOT)/code/lib/include \
$(ROOT)/code/foo/include
LINK_LIBS = startup lib foo
include $(ROOT)/build/make.rule
d. startup/src 目录下面Makefile的写法:
#2012-10-21
EXE =
LIB = libstartup.a
INCLUDE_DIRS = $(ROOT)/code/startup/include \
$(ROOT)/code/foo/include
LINK_LIBS =
include $(ROOT)/build/make.rule
e. foo/src 目录下的Makefile :
#2012-10-21
EXE =
LIB = libfoo.a
INCLUDE_DIRS = $(ROOT)/code/foo/include
LINK_LIBS =
include $(ROOT)/build/make.rule
f. lib/src 目录下的Makefile :
#2012-10-21
EXE =
LIB = liblib.a
INCLUDE_DIRS = $(ROOT)/code/lib/include
LINK_LIBS =
include $(ROOT)/build/make.rule
PS:我们重点注意 我们标色的 地方,其它地方不用看。
⑴
INCLUDE_DIRS = $(ROOT)/code/huge/include \
$(ROOT)/code/startup/include \
$(ROOT)/code/lib/include \
$(ROOT)/code/foo/include
PS:在 huge目录下的 C文件 包含了其它目录下面的头文件时(如 foo/include 下面的 2440addr.h),
main.c(假设在 huge/src下):
#include"2440addr.h"
#include"stdio.h"
............
像这样子的 程序,头文件 在 "别人的目录下面" 你想用它 就必须 用 INCLUDE_DIRS = 去选择它的 路径,但是 它($(ROOT)/code/lib/include ) 必须写在 $(ROOT)/code/huge/include 的后面。
如果 将 上面改成 下面这样,就会出现 "文章开始的错误了" :
INCLUDE_DIRS =$(ROOT)/code/lib/include \
$(ROOT)/code/startup/include \
$(ROOT)/code/huge/include \
$(ROOT)/code/foo/include
错误提示:Makefile:8: *** missing separator (did you mean TAB instead of 8 spaces?). Stop.
同理下面也是一样的.
⑵
INCLUDE_DIRS = $(ROOT)/code/startup/include \
$(ROOT)/code/foo/include