c语言学习笔记二十二

自动处理头文件的依赖关系


all:test34


test34:test34.o test33.o test31.o
         gcc$~ -o $@  
test34.o:test34.c test29.h test30.h test32.h  
test33.o:test33.c test32.h test29.h  
test31.o:test31.c test30.h  test29.h
clean:
      -rm main *.o
.PHONY:clean


生成目标文件和源文件的依赖关系


gcc -M test34.c


yuezhenhua@ubuntu:/opt/sdk/tc/makefile$ gcc -M test34.c
test34.o: test34.c /usr/include/stdio.h /usr/include/features.h \
 /usr/include/i386-linux-gnu/bits/predefs.h \
 /usr/include/i386-linux-gnu/sys/cdefs.h \
 /usr/include/i386-linux-gnu/bits/wordsize.h \
 /usr/include/i386-linux-gnu/gnu/stubs.h \
 /usr/include/i386-linux-gnu/gnu/stubs-32.h \
 /usr/lib/gcc/i686-linux-gnu/4.4.6/include/stddef.h \
 /usr/include/i386-linux-gnu/bits/types.h \
 /usr/include/i386-linux-gnu/bits/typesizes.h /usr/include/libio.h \
 /usr/include/_G_config.h /usr/include/wchar.h \
 /usr/lib/gcc/i686-linux-gnu/4.4.6/include/stdarg.h \
 /usr/include/i386-linux-gnu/bits/stdio_lim.h \
 /usr/include/i386-linux-gnu/bits/sys_errlist.h test29.h test30.h \
 test32.h


如果不需要输出系统文件的依赖关系
gcc -MM *.c


yuezhenhua@ubuntu:/opt/sdk/tc/makefile$ gcc -MM *.c
test31.o: test31.c test30.h test29.h
test33.o: test33.c test32.h test29.h
test34.o: test34.c test29.h test30.h test32.h


把这些规则包含到makefile中
all: test34
test34: test34.o test33.o test31.o
gcc $^ -o $@  
clean:
-rm test34 *.o
.PHONY: clean
sources = test34.c test33.c test31.c
include $(sources:.c=.d) 
%.d: %.c 
set -e; rm -f $@;
$(CC) -MM $(CPPFLAGS) $< > $@.$$$$;
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;
rm -f $@.$$$$


常用的make命令行选项
-n 打印要执行的命令


-c可以切换到另一目录执行该目录下的makefile
-g 可以在命令行定义cflags变量

你可能感兴趣的:(c语言学习笔记二十二)