tags:

views:

636

answers:

2

Hello,

I am compiling my program that will running on linux gcc 4.4.1 C99.

I was just putting my #defines in to separate the code that will be compiled on either windows or linux. However, I god this error.

error: macro names must be identifiers.

Using this code

#ifdef(WIN32)
/* Do windows stuff
#elif(UNIX)
/* Do linux stuff */
#endif

However, when I changed to this the error was fixed:

#if defined(WIN32)
/* Do windows stuff
#elif(UNIX)
/* Do linux stuff */
#endif

I was just wondering why I got that error and why the #defines are different?

Many thanks,

+11  A: 

If you use #ifdef syntax, remove the brackets.

What comes to difference, #if defined(smth) way is more flexible. For example in your case:

#if defined(WIN32) && !defined(UNIX)
/* Do windows stuff */
#elif defined(UNIX) && !defined(WIN32)
/* Do linux stuff */
#else
/* Error, both can't be defined or undefined same time */
#endif
yeah, but you could also cascade #ifdef UNIX with #ifndef WIN32, and get the same flexibility (not as readable, I agree)
jpinto3912
A: 

#ifdef checks whether a macro by that name has been defined, #if evaluates the expression and checks for a true value

#define FOO 1
#define BAR 0

#ifdef FOO
#ifdef BAR
/* this will be compiled */
#endif
#endif

#if BAR
/* this won't */
#endif

#if FOO || BAR
/* this will */
#endif
johannes