6

make continues to build and says everything is up to date when my dependency files say an object depends on a header file that has moved.

If run make -d to capture the evaluation I see:

Considering target file `../build/out/src/manager.o'.
     Looking for an implicit rule for `../build/out/src/manager.o'.
     No implicit rule found for `../build/out/src/manager.o'.
      Pruning file `../product/build/config/product.conf'.
      Pruning file `../build/out/opt_cc.txt'.
      Considering target file `../mem/src/manager.c'.
       Looking for an implicit rule for `../mem/src/manager.c'.
       No implicit rule found for `../mem/src/manager.c'.
       Finished prerequisites of target file `../mem/src/manager.c'.
      No need to remake target `../mem/src/manager.c'.
      Pruning file `../mem/mem.h'.
     Finished prerequisites of target file `../build/out/src/manager.o'.
     Prerequisite `../product/build/config/product.conf' is older than target `../build/out/src/manager.o'.
     Prerequisite `../build/out/opt_cc.txt' is older than target `../build/out/src/manager.o'.
     Prerequisite `../mem/src/manager.c' is older than target `../build/out/src/manager.o'.
     Prerequisite `../mem/mem.h' of target `../build/out/src/manager.o' does not exist.
../build/out/src/manager.o'.
     Prerequisite `../mem/mem_in.h' is older than target `../build/out/src/manager.o'.
    No need to remake target `../build/out/src/manager.o'.

So make knows the file is needed and is not there but doesn't attempt to create it from a rule or fail.

Prerequisite `../mem/mem.h' of target `../build/out/src/manager.o' does not exist.

Why is this and how can I get make to not ignore this rule?

3 Answers 3

8

Most likely you have implemented an automatic dependency generation method that tells make to essentially ignore those files if they don't exist, by defining a target for that file without a rule. When I have this makefile:

foo: foo.h ; @echo make $@ from $^

And no foo.h then make tells me:

$ make
make: **** No rule to make target 'foo.h', needed by 'foo'.  Stop.

But, if I have this makefile:

foo: foo.h ; @echo make $@ from $^
foo.h:

Now make is perfectly happy:

$ make
make foo from foo.h

That's a documented behavior that many auto-dependency generation utilities rely on: if you look in your generated dependency makefiles you'll see one of those empty targets for every header file.

The idea is that given correct dependency information there should never be a way to rename or delete a header file without modifying some other source or header file, which would cause the object file to be rebuilt anyway (hence recreating the dependency information correctly for the next time).

5
  • The dependency file is just "../build/out/src/manager.o : ../mem/mem.h". Running make with -p I find make has decided the header is an intermediate prerequisite? Even when crafting a target for that particular header file. That target isn't run
    – Oliver
    May 30, 2014 at 23:28
  • 1
    I seem to have this working now by defining a rule '%.h : deps' with an empty command and where deps is a phony target
    – Oliver
    May 30, 2014 at 23:39
  • From the make manual: A phony target should not be a prerequisite of a real target file; if it is, its recipe will be run every time make goes to update that file. Checked for versions 3.81 and current editions. Dec 14, 2015 at 14:25
  • @TJOlaes I'm not sure how that relates to this question/answer? There are no phony targets here. Dec 14, 2015 at 18:50
  • 1
    I was replying to @Oliver's comment about his workaround, not your answer. Dec 14, 2015 at 22:34
5

The fix I discovered is to use static pattern rules instead of pattern rules. Pattern rules look like this:

%.o : %.c
    *recipe here*

Static pattern rules only apply to an explicit list of target files like this:

$(OBJECTS): %.o: %.c
    *recipe here*

where the variable OBJECTS is defined earlier in the makefile to be a list of target files (separated by spaces), for example:

OBJECTS := src/fileA.c src/fileB.c src/fileC.c

Note that you can use the various make utility functions to build that list of target files. For example, $(wildcard pattern), $(addsuffix), etc.

You don't show the relevant parts of your makefile, so I can't be sure this will fix your situation. But I was getting the same symptoms of make ignoring non-existant prerequisites, and the same message from make -d about "Prerequisite does not exist" but then make does nothing to create the prerequisite.

Note that make contains about 90 built-in implicit rules, most of which are pattern rules. So even if your makefile has no pattern rules, this could still be affecting you.

I noticed a few things:

  1. Asking to explicitly make the prerequisite works. This shows that make knows how to build the prerequisite.

  2. Adding an explicit target statement (not using a pattern rule) for a particular file seemed to solve the problem. (But of course only for that one target... writing a rule for each of 100's of targets is unworkable).

  3. In my research I kept encountering statements about how make's behavior differs if there is an explicit target or not.

  4. Turning off the implicit rules database with make -r did not help.

My guess why this works is that static pattern rules are a form of explicit target statements for the entire list of targets.

For the particular case that I was investigating, the target file already exists, and the (non-existant) prerequisite is not actually needed to build the target, it is only needed at runtime. So perhaps make is correct about not needing to build the prerequisite in order to build the target. If you are only asking to build the target, then make will do the minimum necessary to accomplish that.

It still seems like a bug in make. But it's probably just a mis-understanding on my part, I'm no make expert.

Update: (March 16, 2016). My current understanding is that make regarded these prerequisite files as intermediate files and therefore does not care about creating them when the target file already exists. Make treats intermediate files as "second class citizens", it only makes them when needed for creating the "first class citizens" which are the explicit target defined in the makefile, or goals requested as arguments to make on the command line.

By using static pattern rules (instead of pattern rules) those files now are listed as explicit targets and are therefore not intermediate files but are "first class citizens" which are created and updated whenever necessary.

BTW, this also explains why using .SECONDARY with no prerequisites doesn't help. Doing that prevents intermediate files from being deleted, but they are still regarded as intermediate so make will not create them unless they are needed to make a target or goal.

2
  • To add to this (three years later...), the GNU Make manual's info on Chained Rules explains the issue, matching the description above. Make considers patterned-matched targets to be intermediates, which it may ignore (and even delete) when making "real" targets. You can still define an implicit rule, but for intermediates that you really want rebuilt, you must have an explicit target for it.
    – Saites
    Mar 11, 2019 at 23:15
  • It is not true that you must have an explicit target for any file that you don't want to be rebuilt. Make will consider targets to be explicit if they're mentioned explicitly in the makefile, at all: it doesn't have to be as a target. It's sufficient, for example, to list them as prerequisites of some other target: now they are no longer considered intermediate. Feb 27, 2022 at 14:29
1

I worked on this issue in my codebase for quite a while, but could not find any cause (or solution) that matched my situation (regardless of how many searches I did on the topic). @owler, with the 'Update: (March 16,2016)' addendum, provided a hint to the actual problem with the statement "[Using .SECONDARY with no prerequisites] prevents intermediate files from being deleted, but they are still regarded as intermediate so make will not create them unless they are needed to make a target or goal."

There is actually an additional characteristic of 'Intermediate' files that was actually causing the problem I was having.

Here is what the makefile looked like:

.SECONDARY:

x.exe: x.o
        $(RECIPESTEP1)

x.o: x.c
        $(RECIPESTEP2)

In my case, x.o was the target that was not being rebuilt (even though it's explicitly listed as a dependency of the make goal x.exe). More importantly (to understand the issue), x.exe did already exist.

The complete behavioral description of 'Intermediate' targets is that 'Intermediate' targets will be built ONLY if they are needed to build a dependent target AND if a dependency of the 'Intermediate' target has to be rebuilt. In my case, the 'x.c' file already exists and has no dependencies, so make decides 'x.c' doesn't need to be rebuilt. Since ALL targets (including 'x.o') are marked as 'Intermediate' (due to the .SECONDARY: target with no dependencies) and since 'x.c' didn't need to be rebuilt, make assumes that an up to date 'x.o' was already used to build 'x.exe' (because 'x.exe' already exists). Therefore, make does not (re)build 'x.o'.

In the end, it was @Owler's comment and (careful) review of the https://www.gnu.org/software/make/manual/html_node/Special-Targets.html reference page that resulted in discovering the solution.

NOTE that I have presented a simplified makefile, but in the actual makefile, x.o was built from an implicit rule based on x.c - static pattern rules did not resolve the issue I was having.

The solution for me was to remove the .SECONDARY: target. Going forward, the plan is to specify .SECONDARY: only with specific dependencies if necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.