可以为我在外部文本文件中展开几个宏吗



我有一个相当大且详细的基于行的配置文件部分。我想使用这个部分作为模板(假设我要预配置这个部分,测试它,然后用$(make)$(macros)替换实际值),有效地替换关键参数(实际上很少);克隆";这个";模板";工作配置文件中几乎没有自定义参数。make能在所描述的情况下为我做这项工作吗?

请耐心等待,我真的是一个make的外行,甚至不确定在这种情况下它是否是正确的工具


一个例子

我正在预配置和测试类似的东西:

<section0>
contains a lot of settings
which were tested and should 
be exactly the same in every copy 
except marked with trailing0 
</section0>

我想知道,如果将标记为零的标记转换为宏:

<$(section)>
contains a lot of settings
which were tested and should 
be exactly the same in every copy 
except marked with $(trailing) 
</$(section)>

想知道我可以利用make来生成预制配置的克隆,用我的数据代替宏进行稍微定制:

<section42>
contains a lot of settings
which were tested and should 
be exactly the same in every copy 
except marked with trailing42
</section42>
<foo>
contains a lot of settings
which were tested and should 
be exactly the same in every copy 
except marked with bar
</foo>

假设"section42""foo""trailing42""bar"分别是$(section)$(trailing)宏的替代品。

您可以在makefile中使用m4预处理器来执行以下操作:在模板文件中展开宏:

M4可以称为"模板语言"、"宏语言"或"预处理器语言"。"m4"这个名字也指的是处理这种语言文本的程序:这个"预处理器"或"宏处理器"将m4模板作为输入,并在对任何称为宏的嵌入式指令进行操作后将其发送到输出。

创建一个名为section.m4:的文件

$ cat section.m4 
<section0>
contains a lot of settings
which were tested and should
be exactly the same in every copy
except marked with trailing0
</section0>

在你的makefile中有一个规则,可以扩展该模板中的宏来生成section.cfg:

section.cfg : section.m4
m4 -Dsection0=foo -Dtrailing0=bar $< >$@

最新更新