English 中文(简体)
职能条件性宏观标准——造成“功能”重新定义
原标题: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

但就我的情况而言,我正在确定一项职能。

#if TARGET_IPHONE_SIMULATOR

#define doSomething(){
    \ does something
}

#else

#define doSomething(){
    \ does something else
}

#endif

这样做确实可行,但我却使编审员屈服于这一警告:

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

是否有任何工作帮助消除警告?

<><>UPDATE:

因此,我试图在我的定义中列入条件:

#define doSomething(){

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

}

但是,这犯了一个错误:

error:  #  is not followed by a macro parameter.
最佳回答

我找到了对我问题的答复: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 }

造成这一错误的:

error:  #  is not followed by a macro parameter.

这是有意义的,因此,我必须重新思考我的执行。

问题回答

Also, this might address the error you received:
# and ## have special purposes inside macro definitions. # is used to surround a macro parameter with double quotes. ## is used to concatenate two macro parameters. Example:
#define ABC(X) #X
ABC(hello) results in "hello".
#define XYZ(X,Y) X##Y
XYZ(O,K) results in OK. Note that this feature is (possibly) unique to ANSI C.
Also, why would you be using a macro like this? Might a function work better for you?

你们的想法是比照/延伸。 罗马语必须被视为类似于功能的宏观。 因此,其定义模糊不清。 摘录如下:

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

考虑的一个选择是,在一个将及时解决的宏观中创造条件。 考虑如下:

If I would like to call a different function based on the value of c as a pre-processor action, I can define a macro that checks the value of c statically.

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

那么,如果你把一个最优化的等级混为一谈,把永远无法达到的分行移走,那么就应该排除哪一种情况。 这并不是你所期待的。





相关问题
gcc -fPIC seems to muck with optimization flags

Following along from this question: how-do-i-check-if-gcc-is-performing-tail-recursion-optimization, I noticed that using gcc with -fPIC seems to destroy this optimization. I am creating a shared ...

Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix. I tried the gcc: gcc -c file.c I also used firstly cpp and then try as but I m getting errors. I m trying to build an ...

Getting rid of pre-compiled headers

OK, I have old Metrowerks code for Mac and Windows where the previous developer used pre-compiled headers for every project that this code base builds. How does one get rid of Pre-compiled headers, ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

How to compile for Mac OS X 10.5

I d like to compile my application for version 10.5 and forward. Ever since I upgraded to Snow Leopard and installed the latest XCode, gcc defaults to 10.6. I ve tried -isysroot /Developer/SDKs/...

热门标签