在要使用VERSION宏的文件中,使用#define预处理命令重新定义VERSION宏,避免与其他变量冲突。
示例代码:
定义在头文件a.h中的变量:
int a = 0;
autoheader生成的config.h文件:
/* Define to the version of this package. */
#define VERSION "1.0.0"
在b.c文件中,同时引用了a.h和config.h文件:
#include "a.h"
#include "config.h"
int main(){
printf("version: %s\n", VERSION);
return 0;
}
在编译时,会提示VERSION宏存在冲突:
b.c: In function ‘main’:
b.c:6:27: error: expected ‘)’ before string constant
printf("version: %s\n", VERSION);
^
config.h:23:23: note: in definition of macro ‘VERSION’
#define VERSION "1.0.0"
^
a.h:1:5: note: previous definition of ‘a’ was here
int a = 0;
我们可以在b.c文件中重新定义VERSION宏,避免冲突:
#define VERSION_MYAPP "1.0.0"
#include "a.h"
#include "config.h"
int main(){
printf("version: %s\n", VERSION_MYAPP);
return 0;
}
这样就可以成功编译并输出版本号了。
上一篇:auto关键字匿名赋值