对于C头文件,您可以防止包含多个头文件,如:
#ifndef MY_FOO_H
#define MY_FOO_H
[...]
#endif我如何在m4中做同样的事情,使得对同一文件的多个include()宏调用只会导致内容被包含一次?
具体地说,我想做一个涉及使用宏changequote的ifdef防护(我不会用dnl的代码弄乱我的代码):
最初,当我执行以下操作时,多个inclusions仍然会破坏引号:
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'发布于 2011-03-18 07:26:32
最直接的方法是对cpp版本进行近乎直译的翻译:
ifdef(`my_foo_m4',,`define(`my_foo_m4',1)dnl
(rest of file here)
')dnl因此,如果定义了my_foo_m4,则文件将扩展为空,否则将计算其内容。
发布于 2013-10-10 23:54:21
我认为你的问题中有两个:-怎么做-为什么在我的情况下不起作用。
方法和你做的差不多,但是你需要引用所有的东西
ifdef(`my_foo_m4',,`define(`my_foo_m4',1)
changequote([,])
')问题是,第二次包含该文件时,引用已更改,因此理论上您应该包含以下文件(您已将引用更改为[,],因此从现在开始包含的所有文件都应使用[,不是吗?):
ifdef([my_foo_m4],,[define([my_foo_m4],1)
changequote([[],])
])但是您包含了与原始引用相同的文件,因此您的ifdef位于符号`my_foo_m4' (可能无效)上,而不是my_foo_m4和else位上
define(`my_foo_m4',1)
changequote([,])不加引号(不在[]之间),因此无论测试结果如何,它都会进行求值,这意味着它使用,调用changequote,即调用
changequote(,)这会禁用引用。
https://stackoverflow.com/questions/5346397
复制相似问题