1. Makefile 中:
MYVERSION = 1.0.00000001 CFLAGS += -DMYVERSION=\"$(MYVERSION )\" all: mainapp mainapp: mainapp.c gcc $(CFLAGS) -o mainapp mainapp.c 2. c 文件中 // mainapp.c #include <stdio.h> #ifdef MYVERSION #define STR(x) #x #define STR1(x) STR(x) static char version[] = STR1(MYVERSION); #endif int main() { #ifdef MYVERSION printf("Program myversion: %s\n", version); #else printf("Program myversion: unknown\n"); #endif return 0; }当运行make
命令时,Makefile会编译mainapp.c
并将其链接成mainapp
,并且源代码中的MYVERSION宏就包含了Makefile中定义的值