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

Ros*_*ers 2 m4

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

#ifndef MY_FOO_H
#define MY_FOO_H

[...]

#endif
Run Code Online (Sandbox Code Playgroud)

如何在m4中做同样的事情,include()以便对同一文件的多个宏调用只会导致内容被包含一次?

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

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

更改quote_file.m4:

ifdef(my_foo_m4,,define(my_foo_m4,1)
changequote([,])
)
Run Code Online (Sandbox Code Playgroud)

更改quote_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'
Run Code Online (Sandbox Code Playgroud)

调用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'
Run Code Online (Sandbox Code Playgroud)

gee*_*aur 5

最直接的方法是对该cpp版本进行几乎字面上的翻译:

ifdef(`my_foo_m4',,`define(`my_foo_m4',1)dnl
(rest of file here)
')dnl
Run Code Online (Sandbox Code Playgroud)

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

  • 实际上,更好的做法是:将dnl @@@@ changecom(`@@@@')changequote(`%<',`%>')``作为文件,替换您的报价并可能更改注释令牌以适合您的项目。请注意,`changecom`仅影响`m4`的注释字符,并且与基础工具的注释字符相同通常不是很好。(在“ m4”接受默认的“#”的地方,出于偏执狂而切换了注释序列。) (2认同)