简体   繁体   English

Gcc预处理器和粘贴

[英]Gcc preprocessor and pasting

I found this snippet on stackoverflow the other day (thanks for that): 我前几天在stackoverflow上找到了这个片段(谢谢你):

#define PLATFORM 3
#define PASTER(x,y) x ## _ ## y
#define EVALUATOR(x,y)  PASTER(x,y)
#define PLATFORMSPECIFIC(fun) EVALUATOR(fun, PLATFORM)

extern void PLATFORMSPECIFIC(somefunc)(char *x);

Compiled with gcc -E, it results in: 用gcc -E编译,结果如下:

# 1 "xx.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "xx.c"





extern void somefunc_3(char *x);

However: 然而:

#define PLATFORM linux
#define PASTER(x,y) x ## _ ## y
#define EVALUATOR(x,y)  PASTER(x,y)
#define PLATFORMSPECIFIC(fun) EVALUATOR(fun, PLATFORM)

extern void PLATFORMSPECIFIC(somefunc)(char *x);

results in: 结果是:

# 1 "xx.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "xx.c"





extern void somefunc_1(char *x);

What can I do to make this return 'somefunc_linux' ?. 我该怎么做才能让它返回'somefunc_linux'? Clang seems to do it right, btw. 克朗似乎做对了,顺便说一句。

If you want to use linux as a name, you can change your compiler options to undefine it: 如果要使用linux作为名称,可以更改编译器选项以取消定义:

gcc -Ulinux

or to be standards compliant: 或符合标准:

gcc -std=c90 -pedantic ... # or -std=c89 or -ansi
gcc -std=c99 -pedantic
gcc -std=c11 -pedantic

See more discussion about why here: Why does the C preprocessor interpret the word "linux" as the constant "1"? 请参阅有关原因的更多讨论: 为什么C预处理器将单词“linux”解释为常量“1”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM