To verify an agent-written GNU Makefile, build once, keep the objects, and edit a C header that only one source file includes. The next make should compile that source, reuse the unrelated object, and relink an executable that reflects the edit. Use a GCC-generated dependency file for each object and include those files in Make so the compiler’s header list becomes part of the build graph. GCC preprocessor options
Last reviewed: 2026-11-22 (Asia/Shanghai).
Give the build one affected object and one control
A clean build can pass with a missing header prerequisite because both object files already need compiling. Keep the objects from the first build to expose that missing edge. In this fixture, adjustment.c reads a constant from adjustment.h, while main.c calls the same function before and after the edit. The function signature and both C files stay fixed during the experiment, so the header edit alone determines which object should recompile.
Use GNU Make and GCC on Linux. Run these commands in a new scratch directory and create the Makefile from the next block in the same directory.
cat > main.c <<'EOF'
#include <stdio.h>
int adjustment(void);
int main(void)
{
printf("%d\n", 40 + adjustment());
return 0;
}
EOF
cat > adjustment.c <<'EOF'
#include "adjustment.h"
int adjustment(void)
{
return ADJUSTMENT;
}
EOF
printf '#define ADJUSTMENT 1\n' > adjustment.h
Load the header edges from GCC depfiles
Start each recipe line with a literal tab. The pattern rule compiles one source to one object and writes the corresponding .d file in the same directory.
CC = gcc
objects = build/main.o build/adjustment.o
build/demo: $(objects)
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
build/%.o: %.c | build
$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -MF $(@:.o=.d) -MT $@ -c $< -o $@
build:
mkdir -p $@
-include $(objects:.o=.d)
-MMD emits dependencies for user headers while compiling. -MF selects the depfile path, and -MT puts the exact object target on the left side of the generated rule. For example, the link recipe depends on build/adjustment.o, so a rule for adjustment.o would attach the header to the wrong target. -MP emits empty rules for the listed headers. These let Make proceed when a header path is obsolete and the patch has updated the includes. GCC still needs to resolve every active include. GCC preprocessor options
-include permits missing depfiles on the first build. Later invocations read the generated rules. Put the directive after build/demo to keep the executable as the default goal. Keep build after the pipe as an order-only prerequisite. Make creates the directory before compiling and uses it for scheduling only. The header in the generated rule is a normal prerequisite, so a newer header can make its object stale. GNU Make manual on makefiles
· GNU Make manual on rules
After the first build, build/adjustment.d should contain these two rules. Check build/main.d as well. Its prerequisites should leave out adjustment.h in this fixture.
build/adjustment.o: adjustment.c adjustment.h
adjustment.h:
Edit the header with the previous build in place
Run the commands in order, stopping on any error. Leave build in place for the entire check. The sleep gives the header edit a distinct modification time on typical local filesystems. If the rebuild is unexpectedly idle, confirm the header timestamp is strictly newer than the dependent object’s timestamp before inspecting the graph. GNU Make manual on prerequisite types
make -j2
./build/demo
make -j2
sleep 1
printf '#define ADJUSTMENT 2\n' > adjustment.h
make -j2
./build/demo
make -j2
| Run | Compile and link activity to look for | Program output |
|---|---|---|
| Initial build | Compile main.c and adjustment.c, then link build/demo | 41 |
| First rerun | No compiler or linker command | — |
After editing adjustment.h | Compile adjustment.c and relink build/demo. Reuse build/main.o. | 42 |
| Final rerun | No compiler or linker command | — |
The fixture produced these results with GNU Make 4.3 and GCC 13.3.0 on Linux. Check the command lines and the program output when you adapt it. If the executable prints the new value after both objects compile, look for a prerequisite that explains the extra rebuild. If the expected object compiles and the output stays old, check whether the link ran and whether the test launched the executable that was just linked.
Diagnose missing depfiles and generated headers
If Make reports success and the binary still prints the old value, open the affected object’s .d file. Check that the target matches the object in the link rule, the edited header appears as a prerequisite, and the Makefile includes that particular depfile. Missing depfiles are easy to miss after a cache restore because the same -include behavior that permits the first build also permits a missing dependency file beside an existing object. Restore each .o with its matching .d from the same successful build, or discard the incomplete build directory and rebuild it. GNU Make manual on including makefiles
To see the failure in the fixture, move build/adjustment.d to saved-adjustment.d after the final rerun. Wait a second, set ADJUSTMENT to 3 in adjustment.h, and repeat make -j2 and ./build/demo. In the fixture, Make reported build/demo up to date and the program still printed 42. Move the depfile back to build/adjustment.d and repeat the commands with the header still set to 3. The dependent object compiles, the link runs, and the program prints 43.
For a generated header, declare an explicit normal prerequisite on each object that needs it and give the header a generation rule with its own inputs. For example, if adjustment.c starts including a generated header, add build/adjustment.o: build/generated_adjustment.h and arrange for the compiler to find that file. The first compilation needs the header before it can write a depfile that mentions it. Exercise this rule in a fresh parallel build and in an incremental build after changing the generator’s input. Use a clean build in a controlled environment
.
If every object rebuilds, inspect the generated prerequisite lists before changing the Makefile. A central header can reach several objects through indirect includes. If the unrelated source has no such edge, check for a shared prerequisite that was touched or a build directory incorrectly listed as a normal prerequisite. Choose GCC’s -MD when the build tracks system headers too. For a compiler flag change, check how the project invalidates previously compiled objects. GNU Make manual on rules
· GCC preprocessor options
Put the expected rebuild in the pull request
Name one changed header, the objects that should recompile, and one object that should stay untouched. Attach the before-and-after output and the corresponding compile and link commands. If you add a CI regression test, require it to fail on the missing-depfile case above. A check that only asserts a zero exit code from Make would miss the stale binary. Assert the new program value and capture the compiler log to verify the rebuild scope. How to verify that an agent-written test catches a fault
Sources checked
- The GCC reference documents user versus system header tracking, output and target naming, and the empty rules produced by
-MP. GCC preprocessor options - Section 3.3 of the GNU Make 4.3 manual documents the inclusion of dependency makefiles and the missing-file behavior of
-include. GNU Make manual on makefiles - Sections 4.3 and 4.14 of the same manual cover normal and order-only prerequisites, directory creation, and automatically generated header prerequisites. GNU Make manual on rules