能够根据模块的修改状况从新编译连接目标代码,保证目标代码都是由最新的模块组成的。html
目标:依赖列表spa
命令code
注意命令左侧是Table制表位htm
语句前加#表示注释blog
举个例子来讲明字符串
gcc_hello_world.c -------------------------------- #include <stdio.h> #include "print.h" int main(void) { print_hello(); return 0; } print.c -------------------------------- #include <stdio.h> #include "print.h" void print_hello(void) { printf("Hello world!\n"); } print.h --------------------------------- #ifndef PRINT_H #define PRINT_H void print_hello(void); #endif
i. Makefile代码:get
hello:gcc_hello_world.o print.o @gcc gcc_hello_world.o print.o -o hello gcc_hello_world.o:gcc_hello_world.c print.h gcc -c gcc_hello_world.c print.o:print.c print.h gcc -c print.c clean: rm -f *.o hello
其中clean是伪目标,在make参数时能够使用,执行结果以下:io
ii. 使用变量编写Makefile编译
OBJ=gcc_hello_world.o print.o hello:$(OBJ) gcc $(OBJ) -o hello gcc_hello_world.o:gcc_hello_world.c print.h gcc -c gcc_hello_world.c print.o:print.c print.h gcc -c print.c clean: $(RM) *.o hello
-------------------------------------------------------------------------------------------------------------class
明天继续宏定义的实验操做。
修改gcc_hello_world.c以下:
#include <stdio.h> #include "print.h" int main(void) { #ifdef MACRO print_hello(); #endif return 0; }
CC=gcc CFLAGS += -DMACRO TARGETS := hello all:$(TARGETS) OBJ=gcc_hello_world.o print.o $(TARGETS):$(OBJ) $(CC) $(CFLAGS) $^ -o $@ gcc_hello_world.o:gcc_hello_world.c print.h gcc -c $(CFLAGS) gcc_hello_world.c print.o:print.c print.h gcc -c print.c clean: $(RM) *.o hello
:= += ?= 参考文章:Makefile 中:= ?= += =的区别
$^ -o $@能够参考makefile使用说明
其实添加宏定义方法和gcc -D的命令同样。