RMFLAGS=-fr
CC=cc
OBJS=hello.o hello1.o
MKDIR=mkdir
DIRS=dirobj direxe
EXE=main
EXE:=$(addprefix direxe/,$(EXE))
OBJS:=$(addprefix dirobj/,$(OBJS))
.PHONY:all clean
all:$(EXE) $(DIRS)
$(DIRS):
$(MKDIR) $@
$(EXE):$(OBJS)
$(CC) -o $@ $^
dirobj/%.o:%.c #moshi
$(CC) -o $@ -c $^
clean:
$(RM) $(RMFLAGS) dirobj
执行结果:
melody@melody-G41D3:~/zmakefile/make4$ make
cc -o dirobj/hello.o -c hello.c
cc -o dirobj/hello1.o -c hello1.c
cc -o direxe/main dirobj/hello.o dirobj/hello1.o
melody@melody-G41D3:~/zmakefile/make4$ ./main
myprint 1!
melody@melody-G41D3:~/zmakefile/make4$ ls
direxe hello1.c hello.c makefile 基础文件
dirobj hello1.h main makefile~ 基础文件~
melody@melody-G41D3:~/zmakefile/make4$ cd dirobj
melody@melody-G41D3:~/zmakefile/make4/dirobj$ ls
hello1.o hello.o
melody@melody-G41D3:~/zmakefile/make4$ make clean
rm -fr dirobj
melody@melody-G41D3:~/zmakefile/make4$ ls
direxe hello1.h main makefile~ 基础文件~
hello1.c hello.c makefile 基础文件