GCC preprocessor and insert

I found this snippet on stackoverflow the other day (thanks for that):

#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:

 # 1 "xx.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "xx.c" extern void somefunc_3(char *x); 

But:

 #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); 

leads to:

 # 1 "xx.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "xx.c" extern void somefunc_1(char *x); 

What can I do to return this 'somefunc_linux' ?. Klang seems to be doing it right, by the way.

+6
source share
1 answer

If you want to use linux as a name, you can change the parameters of your compiler to determine it:

 gcc -Ulinux 

or to comply with standards:

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

Learn more about why here: Why does the C preprocessor interpret the word "linux"? like the constant "1",

+5
source

All Articles