c语言如何在宏macro定义时引用环境变量

2024-07-26  本文已影响0人  CodingCode

举例来说:

$ cat -n test.c
     1  #include <stdio.h>
     2
     3  #define VER_STR "VER-"VER
     4
     5  int main() {
     6      printf("VER_STR=%s\n", VER_STR);
     7      return 0;
     8  }

这里定义一个宏VER_STR,其值将会通过编译时由编译环境VER传入。

看Makefile:

$ cat Makefile

VER=${VERSION}

all: test.c
    cc -DVER=\"$(VER)\" $<

编译:

$ make VERSION=1100
cc -DVER=\"1100\" test.c

$ ./a.out
VER_STR=VER-1100

或者:

$ export VERSION=1200

$ make
cc -DVER=\"1200\" test.c

$ ./a.out
VER_STR=VER-1200
上一篇 下一篇

猜你喜欢

热点阅读