例如test.c文件内容如下:.
#########################################################
#ifdef ABC
char p2[] = "2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222";
#endif
#if ABC == 8
char p3[] = "333333333333333333333333333333333";
#endif
void main(void)
{
char *p = "1111111";
int i=0;
int j=0;
i++;
do{
i++;
if(i==j){
p = 0;
#if ABC == 8
p = p2[i];
#endif
}
}while(1);
}
void _exit(void)
{
do{
}while(1);
}
#########################################################
利用编译的时候再makefile中执行gcc 传入 ABC的值来控制编译的选项。
$(CC) -DABC=1 -c $(TCK).c -o $(TCK).o
利用 -DABC=1,表示编译的时候整个test.c这个为test.o试,宏ABC是有效的并且值为1
也可以利用make 的时候传入参数。参考
makefile文件内容
#########################################################
OBCCJ=arm-none-eabi-objcopy
CC=arm-none-eabi-gcc
$(TCK): $(TCK).o
$(CC) -D$(AAA) -o $(TCK) $(TCK).o
$(TCK).o:$(TCK).c
$(CC) -D$(AAA) -c $(TCK).c -o $(TCK).o
#########################################################
make的脚本文件为test.sh
#########################################################
make TCK=test AAA=ABC=8
########################################################