预处理指令内部定义?

编程入门 行业动态 更新时间:2024-10-21 15:56:28
本文介绍了预处理指令内部定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: C预处理程序:在#define中使用#if吗?

在define的rhs中包含预处理器指令是否有任何技巧?问题是,预处理器将所有rhs折叠成一条长行。但是也许有一个窍门?

Is there any trick to have preprocessor directives inside rhs of define ? The problem is, preprocessor folds all rhs into one long line. But maybe there is a trick ?

在rhs中我想要的例子是

Example of what I'd want in the rhs is

#define MY_CHECK \ #ifndef MY_DEF \ # error MY_DEF not defined \ #endif

? 目的是一个简短之处:使用1行快捷方式而不是多行检查顺序。

? The purpose is a shortness: to have 1-line shortcut instead of multiline sequence of checks.

推荐答案

正如其他人指出的那样,预处理器宏不能扩展为任何其他预处理器指令。如果这样,您通常会在输入中误输入#字符。但是,有时您可以做一些事情来获得想要的东西。如果您想将宏扩展为以下内容:

As others have noted, preprocessor macros cannot expand into any other preprocessor directives; if they do you'll generally get odd errors about stray '#' characters in the input. However, sometimes there are things you can do to get what you want. If you want a macro that expands to something like:

#ifdef SOMETHING ...some code... #endif

其中某些代码不包含任何预处理程序指令,可以定义一个IFDEF宏:

where some code doesn't include any preprocessor directives, you can define an IFDEF macro:

#ifdef SOMETHING #define IFDEF_SOMETHING(X) X #else #define IFDEF_SOMETHING(X) #endif

然后在另一个宏中使用 IFDEF_SOMETHING(...一些代码...)。

and then use IFDEF_SOMETHING(...some code...) in your other macro.

如果您有一个一堆要重复多次的预处理器碎片,您可以将其粘贴到自己的文件中,然后在需要的每个位置使用 #include stuff 。

If you have a bunch of preprocessor cruft that you want to repeat multiple times, you can stick it in its own file and then use #include "stuff" in each spot you need it.

更多推荐

预处理指令内部定义?

本文发布于:2023-11-14 00:38:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585741.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指令   定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!