简体   繁体   中英

Gnu-Make fails to include a makefile

From the docs :

The include directive tells make to suspend reading the current makefile and read one or more other makefiles before continuing. The directive is a line in the makefile that looks like this:

 include FILENAMES...

FILENAMES can contain shell file name patterns.




Now, given a Makefile:

# we define 'include' from the command-line.
ifdef include


# We create the included Makefile
$(shell echo 'all : ;' >makefile.include)

# And we include it.
# The docs say: "FILENAMES can contain shell file name patterns."
include *.include


else


endif

Running, we get:

$ rm -rf makefile.include && make include=1
makefile:6: *.include: No such file or directory


So, what happened here? We clearly had:

  • Created a Makefile to be included, in:

     $(shell echo 'all : ;' >makefile.include)
  • And later included this makefile, given that we can using "shell file name patterns", as quoted above from the documentation - and thus, we have in the makefile:

     include *.include

So, why did Gnu-Make failed to find this newly-created makefile?

The problem you are facing is not of inclusion, but of order of execution.

make is not a procedural language, but instead a declarative one, so order-of-execution sometimes gets a bit tricky.

In your specific case, make will parse the entire Makefile in the first pass, including the include statements, resulting in an erroneous Makefile, since there is no makefile.include yet.

It will then execute the shell command to create the makefile.include (but again it is too late: inclusion would have happened before).

A second run of make will then correctly include the makefile.include .

Note:

The same is true for both directly using include *.include and include $(wildcard *.include) but only the former will result in an error (since the latter will expand the *.include to an empty set which will then (not) be included, whereas the former will try to include the literal *.include - similar to a stray cat *.nonexistent on the shell).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM