Is it possible to define a macro called IPHONE_ONLY for conditional compilation that looks like this:
IPHONE_ONLY -(void)myMethod { //method body } or
IPHONE_ONLY( -(void)myMethod { //method body }) Even though normally you would surround the function with #ifdef, I tested with gcc and indeed the following also works. I don't know if it is standard:
#ifdef IPHONE # define IPHONE_ONLY(...) __VA_ARGS__ #else # define IPHONE_ONLY(...) #endif IPHONE_ONLY(int func(void) { return 12; }) I have never seen anyone code like that though. It is quite common to write such a function like this:
#ifdef IPHONE int func(void) { return 12; } #endif Your editor also would be much happier with this, since it understands functions, but not function body used as a macro parameter!
int x[] = { 1, 2 };. You'd have to go with the solution in my answer to avoid that.Sure you can, but I don't think that this would make your code more readable
#if on_iphone # define IPHONE_ONLY(...) __VA_ARGS__ #else # define IPHONE_ONLY(...) #endif and then you can use the macro the 2nd way you have it in your question.
But this is really ugly and against the visual expectations of anybody who is used to read proper C.
I think you are looking for ifdef:
#ifdef IPHONE_ONLY void myMethod(){ //method body ) #endif #ifdef IPHONE_ONLY # define MY_METHOD() myMethod(); void myMethod() { ...... } #else # define MY_METHOD() #endif in your c code of your project, you call MY_METHOD().
This will avoid you to call myMethod() in that way each time you need it in your code
for(i=0; i<10; i++) { // some thing to do here #ifdef IPHONE_ONLY myMethod(); #endif // other thing to do here } with the definition above you will call your myMethod(); in this way
for(i=0; i<10; i++) { // some thing to do here MY_METHOD(); // other thing to do here } If IPHONE_ONLY is defined then the prepocessor will change the macro MY_METHOD() by the call of the function myMethod();
If IPHONE_ONLY is not defined then the preprocessor will change the MY_METHOD() by nothing. It's like the macro call MY_METHOD() does not exist in your code. and the function void myMethod() will not be defined
#ifdefwork for you?