我可以强制 C 预处理器生成新行吗?

发布于 2024-09-10 17:06:24 字数 1281 浏览 2 评论 0原文

可能的重复:
如何制作预处理器在宏展开结果中插入换行符?

#define IDENTIFIER { /*new line here*/\
            my_multiline(); /*new line here*/\
            macro(); /*new line here*/\
        } /*new line here*/

我可以强制 C 预处理器在其扩展中生成新行吗?我不相信有一个标准的方法可以做到这一点,但我不介意针对 Visual C++ (2008) 和 gcc 的特定于平台的 hack。

我对通过 m4 或单独的预处理器执行此操作不感兴趣。

为什么我问这个

这或多或少只是出于好奇。因为这是一个黑客,我不会在生产中尝试这个,但我想知道我是否能够做到这一点。几天前,我偶然发现了这个问题:

您可以关闭特定位置包含的任何标头的(特定)编译器警告吗?

这是我自己想要解决方案的问题。我考虑过创建一个宏来包含标头,但使用正确的编译指示在包含之前禁用警告,包含标头我想关闭警告,在包含之后启用警告。

当然,我可以创建一个脚本来生成模拟包含以及用于警告和包含的编译指示。

我的第一个问题是,由于#include 是一个预处理器指令,因此宏对于生成它来说有点无用。但后来我找到了这个答案:

有没有办法在另一个#define中执行#define?

有了这个,我相信如果我能够强制编译器在其扩展中生成换行符,我可以生成特殊的包含宏。

我现在在家,但明天上班时我会发布我的代码。

Possible Duplicate:
How can I make the preprocessor insert linebreaks into the macro expansion result?

#define IDENTIFIER { /*new line here*/\
            my_multiline(); /*new line here*/\
            macro(); /*new line here*/\
        } /*new line here*/

Can I force the C pre-processor to generate a new line in it's expansion? I don't believe there's a standard way to do this but I wouldn't mind platform specific hacks for Visual C++ (2008) and gcc.

I'm not interested in doing this through m4 or a separate pre-processor.

Why I'm asking this:

It's more or less just curiosity. Since it's a hack I'm not going to try this in production but I'd like to know if I'm able to do this anyway. A few days ago I stumbled into this question:

Can you turn off (specific) compiler warnings for any header included from a specific location?

This is a question that I'd like a solution myself. I thought about creating a macro for including a header, but with correct pragmas to disable a warning before the including, include the header I'd like to turn warnings off, enabling the warning after the include.

Of course, I could create a script to generate mock includes with the pragma for the warning and the include.

My first problem was that since #include is a pre processor directive a macro would be somewhat useless to generate it. But then I found this answer:

Is there a way to do a #define inside of another #define?

With this I believe I can generate the special include macro if I'm able to force the compiler to generate newlines in it's expansion.

I'm at home right now but I'll post my code tomorrow when I'm at work.

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

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

发布评论

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

评论(1

灼疼热情 2024-09-17 17:06:24

如果您只是想让代码在调试器中更具可读性,则可以使用内联函数而不是#define宏。不幸的是,inline 关键字是 C99 对 C 的补充,并且仅受 ANSI C 下的专有扩展支持(如 MSVC)。

#if __STDC_VERSION__ < 199901L
# if defined(_MSC_VER)
#  define inline __inline
# elseif defined(__GNUC__)
#  define inline __inline__
# endif
#endif

static inline void IDENTIFIER() {
    my_multiline();
    macro();
}

这样做的另一个好处是可以进行类型检查(并且通常不易出错)。

If you're simply aiming to make the code more readable in a debugger, you could use inline functions instead of #defined macros. Unfortunately, the inline keyword is a C99 addition to C and only supported by proprietary extensions under ANSI C (like MSVC).

#if __STDC_VERSION__ < 199901L
# if defined(_MSC_VER)
#  define inline __inline
# elseif defined(__GNUC__)
#  define inline __inline__
# endif
#endif

static inline void IDENTIFIER() {
    my_multiline();
    macro();
}

This has the additional benefit of being type-checked (and generally less prone to error).

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