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

发布于 2024-10-24 08:56:34 字数 1079 浏览 5 评论 0原文

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

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

发布评论

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

评论(2

半枫 2024-10-31 08:56:34

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

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

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

The most straightforward way is an almost-literal translation of the cpp version:

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

So if my_foo_m4 is defined, the file expands to nothing, otherwise its contents are evaluated.

披肩女神 2024-10-31 08:56:34

我认为您实际上有两个问题:
- 如何做
- 为什么它在我的情况下不起作用。

执行此操作的方法几乎与您所做的一样,但是您需要引用所有内容

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

问题是第二次包含该文件时,引用已更改,因此理论上您应该包含以下文件(您已更改对 [,] 的引用,因此您现在包含的所有文件都应该使用 [,]他们?):

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

但是您在原始引用中包含了相同的文件,因此
Youw ifdef 位于符号 `my_foo_m4' 上(可能无效),而不是 my_foo_m4
并且 else 位

define(`my_foo_m4',1)
  changequote([,])

没有被引用(不在 [] 之间),因此无论测试结果是什么,都会进行评估,这意味着它使用 , 调用changequote,即它调用

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

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

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 ?):

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

but you include the same file with the original quote therefore
Youw ifdef is on the symbol `my_foo_m4' (which is probably invalid) not my_foo_m4
and the else bit

define(`my_foo_m4',1)
  changequote([,])

is not quoted (not between []) and so evaluated whatever the result of the test is, meaning it call changequote with , , ie it calls

changequote(,)

Which disable the quote.

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