通过#define 将变量传递给#include
是否可以按照以下方式做一些事情:
#define import_and_other(s)\
something\
#include s
这样:
import_and_other("file.h")
变成:
something
#include "file.h"
基本上,某些文件在包含之前总是需要执行相同的操作,因此我想包装 #include 来执行这些操作。
Is it possible to do something along these lines:
#define import_and_other(s)\
something\
#include s
Such that:
import_and_other("file.h")
becomes:
something
#include "file.h"
Basically, there are certain files that always need the same action taken before they are included, so I want to wrap up #include to perform these actions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不!
C99 标准规定(第 6.10.2 节):
然而,还有另一条规则(第 10.6.3.2 节,关于“# 运算符”):
宏扩展中的“#include s”未满足此约束 - 单词“include”不是类宏函数的参数。
这会阻止您生成(可用的)“#include”指令。无论如何,在宏扩展期间,还有另一个规则(第 10.6.3.4 节):
这意味着您无法从宏扩展的结果生成任何预处理器指令。
可悲的是,你的努力注定会失败。
No!
The C99 standard says (section 6.10.2):
However, there is another rule (section 10.6.3.2, on 'The # Operator') which says:
The '#include s' in the macro expansion fails this constraint - the word 'include' is not a parameter to the macro-like function.
This prevents you from generating a (usable) '#include' directive. In any case, during macro expansion, there is another rule (section 10.6.3.4) which says:
This means that you cannot generate any preprocessor directive from the result of a macro expansion.
Your effort is, sadly, doomed to failure.
不,但您可以创建自己的包含文件来执行这两项操作,然后 #include 它。使用您选择的 import_and_other 名称命名您的头文件。 #define 替换文本实际上是多行的,它只是为了您的利益而包装的。
No, but you can make your own include file that does both, and then #include it. Name your header file with the import_and_other name of your choice. The #define replacement text is note REALLY multi-line, it just is wrapped for your benefit.
我对此表示怀疑。但可以通过包含另一个文件并在该文件中执行您想要的操作来实现相同的目的。这将实现您想要做的事情。
I doubt it. But can achieve the same by including another file and do what ever you want in that file. This will achieve what you are trying to do.