利用gcc编译选项和宏定义控制代码条件编译

示例代码(文件compile_macro_test.c):

#include <stdio.h>

int main()
{

        int a = 0;
#ifdef DBUG
        a = 5;
#endif

        printf("=========%d\n",a);

        return 0;
}

对应Makefile文件

all:a.out
debug:b.out
        mv b.out a.out

a.out:compile_macro_test.c
        gcc $< -o $@
b.out:compile_macro_test.c
        gcc -DDBUG $< -o $@
.PHOHY:clean
clean:
         rm *.out

运行make后得到的a.out输出为=========0

运行make debug后得到的a.out输出为=========5

posted @ 2012-05-31 10:23  sky-zhang  阅读(3577)  评论(1)    收藏  举报