如何为 m4 宏文件包含 ifdef 保护?
对于 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_invocal.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_invocal.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'
For C header files, you can prevent multiple inclusion of a header file like:
#ifndef MY_FOO_H
#define MY_FOO_H
[...]
#endif
How can I do the same thing in m4 such that multiple include()
macro calls to the same file will only cause the contents to be included once?
Specifically, I want to do an ifdef guard that involves using macro changequote
( I'll not clutter my code with dnl
's):
Originally, when I do the following, multiple inclusions still corrupts the quotes:
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'
Invoked with m4 changequote_invocation.m4
yields:
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最直接的方法是 cpp 版本的几乎字面翻译:
因此,如果定义了 my_foo_m4,则文件将扩展为空,否则将评估其内容。
The most straightforward way is an almost-literal translation of the
cpp
version:So if
my_foo_m4
is defined, the file expands to nothing, otherwise its contents are evaluated.我认为您实际上有两个问题:
- 如何做
- 为什么它在我的情况下不起作用。
执行此操作的方法几乎与您所做的一样,但是您需要引用所有内容
问题是第二次包含该文件时,引用已更改,因此理论上您应该包含以下文件(您已更改对
[
,]
的引用,因此您现在包含的所有文件都应该使用[
,]
他们?):但是您在原始引用中包含了相同的文件,因此
Youw ifdef 位于符号
`my_foo_m4'
上(可能无效),而不是my_foo_m4
并且 else 位
没有被引用(不在 [] 之间),因此无论测试结果是什么,都会进行评估,这意味着它使用
,
调用changequote,即它调用Which 禁用引用。
I think there are in fact 2 question in yours :
- How to do it
- why it doesn't work in my case.
The way to do it is almost as you've done, but you need to quote everything
The problem is the second time you include the file, the quote have been changed, so you should in theory include the following file (you've change the quote to
[
,]
so all the files you include from now should use[
,]
shouldn't they ?):but you include the same file with the original quote therefore
Youw ifdef is on the symbol
`my_foo_m4'
(which is probably invalid) notmy_foo_m4
and the else bit
is not quoted (not between []) and so evaluated whatever the result of the test is, meaning it call changequote with
,
, ie it callsWhich disable the quote.