通过#define 将变量传递给#include

发布于 2024-08-19 06:10:21 字数 289 浏览 3 评论 0原文

是否可以按照以下方式做一些事情:

#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 技术交流群。

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

发布评论

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

评论(3

小梨窩很甜 2024-08-26 06:10:21

不!

C99 标准规定(第 6.10.2 节):

表单的预处理指令

# include pp-tokens 换行符

允许

(与前面两种形式之一不匹配)。预处理
包含在指令中后的标记将像普通文本一样进行处理。 (每个
当前定义为宏名称的标识符被其替换列表替换
预处理令牌。)所有替换后产生的指令应匹配以下之一
前两种形式。

然而,还有另一条规则(第 10.6.3.2 节,关于“# 运算符”):

类函数宏的替换列表中的每个 # 预处理标记应为
后跟一个参数作为替换列表中的下一个预处理标记。

宏扩展中的“#include s”未满足此约束 - 单词“include”不是类宏函数的参数。

这会阻止您生成(可用的)“#include”指令。无论如何,在宏扩展期间,还有另一个规则(第 10.6.3.4 节):

不处理生成的完全宏替换的预处理标记序列
作为预处理指令,即使它类似于预处理指令。

这意味着您无法从宏扩展的结果生成任何预处理器指令。

可悲的是,你的努力注定会失败。

No!

The C99 standard says (section 6.10.2):

A preprocessing directive of the form

# include pp-tokens new-line

(that does not match one of the two previous forms) is permitted. The preprocessing
tokens after include in the directive are processed just as in normal text. (Each
identifier currently defined as a macro name is replaced by its replacement list of
preprocessing tokens.) The directive resulting after all replacements shall match one of
the two previous forms.

However, there is another rule (section 10.6.3.2, on 'The # Operator') which says:

Each # preprocessing token in the replacement list for a function-like macro shall be
followed by a parameter as the next preprocessing token in the replacement list.

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:

The resulting completely macro-replaced preprocessing token sequence is not processed
as a preprocessing directive even if it resembles one.

This means that you cannot generate any preprocessor directive from the result of a macro expansion.

Your effort is, sadly, doomed to failure.

山色无中 2024-08-26 06:10:21

不,但您可以创建自己的包含文件来执行这两项操作,然后 #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.

音栖息无 2024-08-26 06:10:21

我对此表示怀疑。但可以通过包含另一个文件并在该文件中执行您想要的操作来实现相同的目的。这将实现您想要做的事情。

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.

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