访问预处理器宏定义的值

发布于 2024-09-10 06:54:21 字数 363 浏览 2 评论 0原文

如果我在 GCC_PREPROCESSOR_DEFINITIONS 下添加宏“FOO=bar”(如果使用 XCode,则添加预处理器宏),那么访问“FOO”值的最佳方法是什么?

目前,我使用笨拙的:

    #define MACRO_NAME(f) #f
    #define MACRO_VALUE(f)  MACRO_NAME(f)

    #ifdef FOO
        NSLog(@"%s", MACRO_VALUE(FOO));
    #else
        NSLog(@"undefined");
    #endif

这将输出“bar” “

当然,一定有更好/更干净的方法吗?

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"?

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"

Surely, there must be a better/cleaner way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唱一曲作罢 2024-09-17 06:54:21

您正在做的是字符串化(或字符串化)宏值的方式。间接性是不可避免的。

GCC预处理器 Rob 链接到的手册部分(存档链接)

 #define xstr(s) str(s)
 #define str(s) #s
 #define foo 4
 str (foo)
      ==> "foo"
 xstr (foo)
      ==> xstr (4)
      ==> str (4)
      ==> "4

What you are doing is the way to stringize (or stringify) macro values. The indirection is unavoidable.

This is mentioned in the GCC preprocessor manual section (archived link) that Rob linked to:

 #define xstr(s) str(s)
 #define str(s) #s
 #define foo 4
 str (foo)
      ==> "foo"
 xstr (foo)
      ==> xstr (4)
      ==> str (4)
      ==> "4
明媚如初 2024-09-17 06:54:21
NSLog(@"%s", #FOO);

请参阅字符串化。这是您已经在使用的技术。有什么问题吗?

NSLog(@"%s", #FOO);

See Stringification. It's the technique you're already using. What was wrong with it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文