Conditional macroC # define for function, calling: "function" overridden warning

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

but in my case I am defining a function inside a condition.

#if TARGET_IPHONE_SIMULATOR

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

#else

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

#endif

This works, except that I call the gcc compiler for this warning:

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

Is there a workaround to help get rid of warnings?

UPDATE:

So I tried to include a condition inside my definition:

#define doSomething(){\

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

}\

but this causes an error:

error: '#' is not followed by a macro parameter.
+3
source share
4 answers

I found the answer to my question here .

: #ifdef .. #define, .

, '\', , :

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

:

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

, .

+6

, /. doSomething() . , . . :

doSomething() {
#if TARGET_IPHONE_SIMULATOR
   // conditionally compiled code
#else
   // platform-specific code
#endif
}
+1

, , :
# ## . # . ## . :
#define ABC(X) #X
ABC(hello) "hello".
#define XYZ(X,Y) X##Y
XYZ(O,K) OK. , () ANSI C.
, ? ?

+1

- , . :

If I would like to call another function based on the value of 'c' as a preprocessing action, I can define a macro that statically checks the value of 'c'.

#define AorB(c) ((c>0) ? (Do_A(c)) : (Do_B(c)))

Then, if you set up an optimization level that removes branches that are never reachable, it should strip away that has never been executed. This may not be exactly what you were looking for.

0
source

Source: https://habr.com/ru/post/1789381/


All Articles