gpt4 book ai didi

m4 - 如何为 m4 宏文件包含 ifdef 保护?

转载 作者:行者123 更新时间:2023-12-04 04:59:43 25 4
gpt4 key购买 nike

对于 C 头文件,您可以防止多个头文件包含,例如:

#ifndef MY_FOO_H
#define MY_FOO_H

[...]

#endif

我怎样才能在 m4 中做同样的事情,使得对同一个文件的多个 include() 宏调用只会导致内容被包含一次?

具体来说,我想做一个涉及使用宏 changequote 的 ifdef 保护(我不会用 dnl 弄乱我的代码):

最初,当我执行以下操作时,多个包含仍然会破坏引号:

changequote_file.m4:

ifdef(my_foo_m4,,define(my_foo_m4,1)
changequote([,])
)

changequote_invocation.m4:

include(changequote_file.m4)
After first include invocation:
[I should not have brackets around me]
`I should have normal m4 quotes around me'
include(changequote_file.m4)
After second include invocation:
[I should not have brackets around me]
`I should have normal m4 quotes around me'

使用 m4 changequote_invocation.m4 调用产生:

After first include invocation:
I should not have brackets around me
`I should have normal m4 quotes around me'


After second include invocation:
[I should not have brackets around me]
`I should have normal m4 quotes around me'

最佳答案

最直接的方法是cpp版本的几乎直译:

ifdef(`my_foo_m4',,`define(`my_foo_m4',1)dnl
(rest of file here)
')dnl

因此,如果定义了 my_foo_m4,则文件将展开为空,否则将评估其内容。

关于m4 - 如何为 m4 宏文件包含 ifdef 保护?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5346397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com