我想为下面的架构开发一个通用的makefile:
30/04/2015 18:10 <REP> .
30/04/2015 18:10 <REP> ..
30/04/2015 18:08 <REP> exe
30/04/2015 17:22 <REP> headers
30/04/2015 17:53 726 makefile.txt
30/04/2015 18:08 <REP> objects
30/04/2015 17:53 <REP> sources
使用:
来源的内容
30/04/2015 17:53 <REP> .
30/04/2015 17:53 <REP> ..
30/04/2015 17:22 124 main.c
和标题的内容:
30/04/2015 17:22 <REP> .
30/04/2015 17:22 <REP> ..
30/04/2015 17:22 20 myheader.h
对象:输出目标文件的内容。
exe:内容可执行文件
当我将头文件和源放在同一目录中时,makefile会生成输出。但是当我将它们分开时,make会在依赖期间产生错误。
这是我当前的makefile:
SRCDIRS = ../Mydirectory/sources
HDRDIRS = ../Mydirectory/headers
SRCEXTS = .c
HDREXTS = .h
OUTPUT = Output
CC= gcc
SOURCES = $(foreach file,$(SRCDIRS),$(wildcard $(addprefix $(file)/*,.c)))
HEADERS = $(foreach file,$(HDRDIRS),$(wildcard $(addprefix $(file)/*,.h)))
OBJS = $(addsuffix .o, $(basename $(SOURCES)))
DEPS = $(OBJS:.o=.d)
DEP_OPT = -M
DEPEND = $(CC) $(DEP_OPT)
DEPEND.d = $(subst -g ,,$(DEPEND))
COMPILE.c = $(CC) -c -I $(HDRDIRS)
LINK.c = $(CC)
all: $(OUTPUT)
%.d:%.c
[TAB]@echo -n $(dir $<) > $@
@$(DEPEND.d) $< >> $@
objs:$(OBJS)
%.o:%.c
[TAB]$(COMPILE.c) $< -o $@
$(OUTPUT):$(OBJS)
[TAB]$(LINK.c) $(OBJS) -o $@
clean:
[TAB]$(RM) $(OBJS) $(OUTPUT).exe
更新: makefile位于Mydirectory中。
make -f makefile.txt的输出:
在MS-DOS中:
gcc -c -I ../Mydirectory/headers ../Mydirectory/sources/main.c -o ../Mydirectory/sources/main.o
gcc ../Mydirectory/sources/main.o -o Output
创建了两个文件:
objects/main.o
Mydirectory/Output.exe