一共3个文件:
Makefile 、macro_test.c、test.mk
Makefile
include ./test.mk #读取当前目录下test.mk里的宏控
CC = gcc
RM = rm
#判断读到的宏值=y?
ifeq ($(one),y)
CFLAGS += -D one #-D选项就是在编译时增加对-D后面的宏的定义 相当于 gcc -D one
endif
TARGETS := macro
#all:$(TARGETS)
$(TARGETS):macro_test.c
gcc $(CFLAGS) $^ -o $@
macro_test.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
#ifdef one
printf("#ifdef one\n");
#else
printf("#ifdef two\n");
#endif
exit(0);
}
test.mk
#设定要不要编译 y or n
one=y