Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional macro #define for a function - causing: "function" redefined warning

I just saw this thread, describing how to add conditional macros: Conditional value for a #define

but in my case I am defining a function within the condition.

#if TARGET_IPHONE_SIMULATOR

#define doSomething(){\
    \\ does something
}\

#else

#define doSomething(){\
    \\ does something else
}\

#endif

This does work, except I is causing gcc compiler to throw this warning:

"doSomething" redefined
This is the location of the previous arguments

Is there any workaround to help getting rid of the warnings?

UPDATE:

So I tried including the condition inside my definition:

#define doSomething(){\

#if TARGET_IPHONE_SIMULATOR
    \\ do something
#else 
    \\ do something else
#endif

}\

but that throws an error:

error: '#' is not followed by a macro parameter.
like image 238
Bach Avatar asked Jan 20 '23 14:01

Bach


1 Answers

I found the answer to my question here.

Conclusion: you cannot include #ifdef etc... inside #define, because there should only be one pre-processing directive per line.

So although we can break the line with a backslash '\' this helps writing readable multiline macros, but the preprocessor will see it as one line:

#define doSomething(){ #if TARGET_IPHONE_SIMULATOR ... #endif }

Which throws this error:

error: '#' is not followed by a macro parameter.

That makes sense, so I will have to rethink my implementation.

like image 71
Bach Avatar answered Jan 30 '23 12:01

Bach