在我的程序中,我有一个有点复杂的构建过程。目前,在一个目录中,我在 Makefile.am
中使用 include
和一个文件,该文件不存在,但必须自行构建。原因是这个包含文件相当长。此外,在实际程序中,它不仅仅是一个文件,而是多个文件,并且该文件的生成过程可能会不时发生变化。
Makefile.am
看起来像这样
noinst_LIBRARIES = libtest.a
nodist_libtest_a_SOURCES = file.c
CLEANFILES = file.c Make_file.mk
$(builddir)/Make_test.mk: $(srcdir)/Perl/generate_mk_files.pl
perl $(srcdir)/Perl/generate_mk_files.pl file
include $(builddir)/Make_file.mk
创建Make_file.mk
后,它看起来像
$(builddir)/file.c: $(srcdir)/file.template $(srcdir)/Perl/generate_c.pl
perl $(srcdir)/Perl/generate_c.pl $(srcdir)/file.template
Automake 和最终的构建过程都有效。 make
的输出类似于(我稍微缩短了它):
Makefile:721: Make_file.mk: Datei oder Verzeichnis nicht gefunden (file not found)
perl ../../../../src/components/test/Perl/generate_mk_files.pl test
perl ../../../../src/components/test/Perl/generate_c.pl ../../../../src/components/test/file.template
因此,make
首先提示找不到包含文件,然后创建它,然后也遵循包含文件的规则。
虽然我很高兴它有效,但我想知道为什么。首先,我认为 make
加载 Makefile
。在此步骤中,Make_file.mk
不存在。因此,Makefile
似乎被加载了不止一次。
此外,manual Automake 的 include
状态:
Note that these fragments are read and interpreted by automake, not by make.
这不是我所看到的,因为包含的片段在 Automake 执行期间不存在。
我的问题基本上是:
- 它为什么有效?
- 这是执行此操作的正确方法还是我应该使用其他方法,例如在
Makefile
中启动make
的新实例。
最佳答案
我不太了解 Automake,但是,来自 GNU make 手册:
If an included makefile cannot be found in any of these directories {standard includes directories} , a warning message is generated, but it is not an immediately fatal error; processing of the makefile containing the include continues. Once it has finished reading makefiles, make will try to remake any that are out of date or don’t exist. See How Makefiles Are Remade. Only after it has tried to find a way to remake a makefile and failed, will make diagnose the missing makefile as a fatal error.
If you want make to simply ignore a makefile which does not exist or cannot be remade, with no error message, use the -include directive instead of include, like this:
-include filenames…
This acts like include in every way except that there is no error (not even a warning) if any of the filenames (or any prerequisites of any of the filenames) do not exist or cannot be remade.
所以基本上,make 在完成解析主 Makefile 之前无法执行重新生成包含文件的配方。因此它会发出警告,继续读取Makefile,找到重新制作包含文件的规则,重新制作它,然后重新启动自己(这在如何重新制作Makefile部分中有详细解释)。
关于makefile - 通过相同的 Makefile 生成 Makefile 的包含文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42411197/