xcode 访问预处理器宏定义的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3261763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing the value of a Preprocessor Macro definition
提问by Adriaan Tijsseling
If I add a macro "FOO=bar" under GCC_PREPROCESSOR_DEFINITIONS (or Preprocessor Macros if you use XCode"), what would be the best way to access the value of "FOO"?
如果我在 GCC_PREPROCESSOR_DEFINITIONS(或预处理器宏,如果您使用 XCode)下添加宏“FOO=bar”,那么访问“FOO”值的最佳方法是什么?
Currently, I use the clumsy:
目前,我使用笨拙的:
#define MACRO_NAME(f) #f
#define MACRO_VALUE(f) MACRO_NAME(f)
#ifdef FOO
NSLog(@"%s", MACRO_VALUE(FOO));
#else
NSLog(@"undefined");
#endif
This will output "bar"
这将输出“bar”
Surely, there must be a better/cleaner way?
当然,必须有更好/更清洁的方法吗?
采纳答案by Georg Fritzsche
What you are doing is the wayto stringize(or stringify) macro values. The indirection is unavoidable.
你在做什么的方式来stringize(或字符串化)宏值。间接性是不可避免的。
This is mentioned in the GCC preprocessor manual section (archived link)that Rob linked to:
Rob 链接到的 GCC 预处理器手册部分(存档链接)中提到了这一点:
#define xstr(s) str(s)
#define str(s) #s
#define foo 4
str (foo)
==> "foo"
xstr (foo)
==> xstr (4)
==> str (4)
==> "4
回答by Rob Napier
NSLog(@"%s", #FOO);
See Stringification. It's the technique you're already using. What was wrong with it?
请参阅字符串化。这是您已经在使用的技术。出了什么问题?