对于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)
最直接的方法是对该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已定义,则文件将扩展为空,否则将评估其内容。