7

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

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' ?. Clang seems to do it right, btw.

KJH
  • 375
  • 2
  • 10
  • 3
    Also state your compiler versions. I get the same results with clang and gcc - as both compilers predefines a `#define linux 1` – nos Apr 28 '14 at 19:35
  • Ah.. good call. I didn't check to see whether linux was predefined. – KJH Apr 28 '14 at 19:37
  • I thought that on Linux, it was only 'linux' with two underscores at either end. My bad. – KJH Apr 28 '14 at 19:46
  • Check out http://stackoverflow.com/questions/19210935/why-does-the-c-preprocessor-interpret-the-word-linux-as-the-constant-1 - lots of discussion on this. – mbonneau Apr 28 '14 at 19:47
  • I think it's all the more reason to have a special token to mean 'evaluate this' - like, say, a dollar-sign or something. It works for shells... – KJH May 05 '14 at 19:35

1 Answers1

5

If you want to use linux as a name, you can change your compiler options to undefine it:

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"?

Community
  • 1
  • 1
mbonneau
  • 627
  • 3
  • 8