Your First Makefile: Rules, Targets, and Prerequisites

Write a Makefile from scratch for a multi-file C program: rule anatomy, the TAB rule, timestamp rebuilds, object files, phony targets, recipe prefixes and dry runs.

By The Dark DebuggerUpdated 22 min read

In this article we write a Makefile for a small C program, one rule at a time, and run every step. By the end you'll know how a rule is put together, why Make rebuilds what it rebuilds, how to split a build into object files so a one-line change doesn't recompile everything, and the habits (.PHONY, @, -, make -n) that every real Makefile uses. You need a working GNU Make and GCC from Installing GNU Make, and the ideas from Introduction to Make. You don't need to be able to write C; I'll explain the compiler commands as we go.

The project

logstat reads a web server access log and counts responses by status class. It's split across three source files the way real C programs are:

.
├── README.md
├── access.log
├── main.c
├── parse.c
├── parse.h
├── report.c
└── report.h

parse.c turns one log line into a struct, report.c keeps the counts and prints them, and main.c reads the file and ties the two together. The .h header files declare what each .c file provides, so the others can use it:

/* parse.h */
#ifndef PARSE_H
#define PARSE_H
 
struct log_entry {
    char ip[46];
    int status;
    long bytes;
};
 
/* Parse one combined-log-format line. Returns 1 on success, 0 otherwise. */
int parse_line(const char *line, struct log_entry *entry);
 
#endif
/* parse.c */
#include <stdio.h>
#include "parse.h"
 
int parse_line(const char *line, struct log_entry *entry)
{
    int n = sscanf(line, "%45s %*s %*s [%*[^]]] \"%*[^\"]\" %d %ld",
                   entry->ip, &entry->status, &entry->bytes);
    return n == 3;
}
/* report.h */
#ifndef REPORT_H
#define REPORT_H
 
#include <stdio.h>
#include "parse.h"
 
#define REPORT_TITLE "logstat report"
 
struct stats {
    long requests;
    long bytes;
    long by_class[6];   /* by_class[2] counts 2xx responses, and so on */
};
 
void stats_add(struct stats *s, const struct log_entry *e);
void print_report(const struct stats *s, FILE *out);
 
#endif
/* report.c */
#include "report.h"
 
void stats_add(struct stats *s, const struct log_entry *e)
{
    int class = e->status / 100;
 
    s->requests++;
    s->bytes += e->bytes;
    if (class >= 1 && class <= 5)
        s->by_class[class]++;
}
 
void print_report(const struct stats *s, FILE *out)
{
    fprintf(out, "%s\n", REPORT_TITLE);
    fprintf(out, "requests: %ld\n", s->requests);
    fprintf(out, "bytes:    %ld\n", s->bytes);
    for (int c = 2; c <= 5; c++)
        fprintf(out, "%dxx:      %ld\n", c, s->by_class[c]);
}
/* main.c */
#include <stdio.h>
#include "parse.h"
#include "report.h"
 
int main(int argc, char **argv)
{
    FILE *in = stdin;
    char line[4096];
    struct stats s = {0};
    struct log_entry e;
 
    if (argc > 1 && (in = fopen(argv[1], "r")) == NULL) {
        perror(argv[1]);
        return 1;
    }
    while (fgets(line, sizeof line, in) != NULL) {
        if (parse_line(line, &e))
            stats_add(&s, &e);
        else
            fprintf(stderr, "skipping malformed line: %s", line);
    }
    print_report(&s, stdout);
    return 0;
}

You don't need to follow every line of C. What matters for Make is who includes what:

FileIncludes
main.cparse.h, report.h
parse.cparse.h
report.creport.h, which itself includes parse.h

The log itself is ordinary combined-format lines:

203.0.113.7 - - [09/Nov/2026:10:01:12 +0000] "GET /index.html HTTP/1.1" 200 5120 "-" "curl/8.5.0"
198.51.100.23 - - [09/Nov/2026:10:01:15 +0000] "GET /api/orders HTTP/1.1" 200 932 "-" "Mozilla/5.0"
203.0.113.7 - - [09/Nov/2026:10:01:19 +0000] "POST /api/login HTTP/1.1" 401 112 "-" "curl/8.5.0"
[…]

Built by hand, it's one compiler command:

gcc -Wall -Wextra -o logstat main.c parse.c report.c
./logstat access.log
logstat report
requests: 6
bytes:    6404
2xx:      2
3xx:      1
4xx:      2
5xx:      1

-Wall -Wextra turns on the compiler's useful warnings, and -o logstat names the output program. Now let's make Make run that for us.

Anatomy of a rule

A Makefile is a list of rules. Every rule has the same shape:

target: prerequisite1 prerequisite2 ...
<TAB>recipe line 1
<TAB>recipe line 2
PartMeaning
targetThe file this rule produces (or, for phony targets, a command name)
prerequisitesFiles the target is built from. If any is newer than the target, the target is rebuilt
recipeShell commands that build the target, each line starting with a TAB

Here is the first version of our Makefile, saved as Makefile next to the sources:

logstat: main.c parse.c report.c parse.h report.h
	gcc -Wall -Wextra -o logstat main.c parse.c report.c

The headers are listed as prerequisites even though they don't appear in the command. They don't need to: a prerequisite list says "rebuild if any of these change", and a change to report.h changes the program just as surely as a change to report.c.

make
make
gcc -Wall -Wextra -o logstat main.c parse.c report.c
make: 'logstat' is up to date.

The first run built the program. The second compared timestamps, found logstat newer than all five prerequisites, and did nothing. Touch one source file and it rebuilds:

touch report.c
make
gcc -Wall -Wextra -o logstat main.c parse.c report.c

That works, but notice the cost: a change to report.c recompiled all three files. With three tiny files you won't notice. With three hundred, you will. We'll fix it shortly.

The TAB rule

The recipe line above starts with a TAB character. Not four spaces, not eight: a TAB. If your editor converts it to spaces, Make can't tell the line is a recipe:

cat -A Makefile
make
logstat: main.c parse.c report.c parse.h report.h$
    gcc -Wall -Wextra -o logstat main.c parse.c report.c$
Makefile:2: *** missing separator.  Stop.

cat -A shows TABs as ^I, and here there aren't any. "Missing separator" means Make tried to read line 2 as a rule or variable assignment and found neither a : nor an =. Every Make user hits this once. Installing GNU Make shows how to stop your editor doing it.

The default goal

When you type make with no arguments, Make builds the first target in the file. That's called the default goal. It sounds like trivia until someone adds a rule to the top of the Makefile. Here, a clean rule was added above the rule for logstat:

clean:
	rm -f logstat *.o
make
ls
rm -f logstat *.o
Makefile
README.md
access.log
main.c
parse.c
parse.h
report.c
report.h

A plain make deleted the program. You can always name the target you want (make logstat), but the convention every project follows is to make the first rule a target called all that depends on whatever "build everything" means:

all: logstat

(GNU Make also has a .DEFAULT_GOAL variable to set the default explicitly, but an all rule at the top is what readers expect to see.)

Splitting the build into object files

The fix for "everything recompiles" is to compile each .c file separately into an object file (.o), then link the object files into the program. gcc -c file.c compiles without linking and writes file.o. Linking object files is fast; compiling is the slow part, so we only want to compile what changed.

logstat: main.o parse.o report.o
	gcc -o logstat main.o parse.o report.o
 
main.o: main.c parse.h report.h
	gcc -Wall -Wextra -c main.c
 
parse.o: parse.c parse.h
	gcc -Wall -Wextra -c parse.c
 
report.o: report.c report.h parse.h
	gcc -Wall -Wextra -c report.c

Each object file lists its own .c file and every header that file includes, directly or indirectly. That table of who-includes-what turned into prerequisites. The dependency graph now has two levels:

                      logstat
          /              |              \
      main.o          parse.o          report.o
    /   |    \        /     \        /    |     \
main.c parse.h report.h  parse.c parse.h  report.c report.h parse.h

Build from clean, then build again:

make
make
gcc -Wall -Wextra -c main.c
gcc -Wall -Wextra -c parse.c
gcc -Wall -Wextra -c report.c
gcc -o logstat main.o parse.o report.o
make: 'logstat' is up to date.

Make had to build all three objects before it could link, so it worked depth-first through the graph. Now change one source file:

touch report.c
make
gcc -Wall -Wextra -c report.c
gcc -o logstat main.o parse.o report.o

One compile and a link. That's the payoff. Now change a header:

touch report.h
make
ls -l --time-style=+%H:%M:%S *.o logstat
gcc -Wall -Wextra -c main.c
gcc -Wall -Wextra -c report.c
gcc -o logstat main.o parse.o report.o
-rwxr-xr-x 1 sid sid 16488 21:55:35 logstat
-rw-r--r-- 1 sid sid  2448 21:55:35 main.o
-rw-r--r-- 1 sid sid  1600 21:55:31 parse.o
-rw-r--r-- 1 sid sid  2136 21:55:35 report.o

main.c and report.c include report.h, so both recompiled. parse.o keeps its older timestamp because parse.c doesn't include report.h. Make got this right only because the prerequisite lists are right. Keeping them right by hand gets tedious; in Parallel Builds, Dependency Generation, and Debugging we'll have the compiler generate them.

clean, and why .PHONY exists

Every project needs a way to delete build output. Add an all rule at the top and a clean rule at the bottom:

all: logstat
 
logstat: main.o parse.o report.o
	gcc -o logstat main.o parse.o report.o
 
main.o: main.c parse.h report.h
	gcc -Wall -Wextra -c main.c
 
parse.o: parse.c parse.h
	gcc -Wall -Wextra -c parse.c
 
report.o: report.c report.h parse.h
	gcc -Wall -Wextra -c report.c
 
clean:
	rm -f logstat *.o
make
make clean
make
make: Nothing to be done for 'all'.
rm -f logstat *.o
gcc -Wall -Wextra -c main.c
gcc -Wall -Wextra -c parse.c
gcc -Wall -Wextra -c report.c
gcc -o logstat main.o parse.o report.o

clean has no prerequisites and never creates a file called clean, so the target is always "missing" and the recipe always runs. That works until a file named clean appears in the directory. It sounds unlikely, but generated files and stray redirects do it more often than you'd think:

touch clean
make clean
make: 'clean' is up to date.

Make found a file called clean, saw that it has no prerequisites to be older than, and decided there was nothing to do. Your build output is still there. The fix is to declare which targets are phony, meaning names of actions, not files:

.PHONY: all clean

Put it near the top. Make then ignores any file with those names and always runs their recipes when asked:

make clean
rm -f logstat *.o

Declare every target that doesn't produce a file of the same name: all, clean, test, install, run, dist. Besides fixing the stray-file problem, it tells the next reader of the Makefile which targets are files and which are commands.

Recipe prefixes: @ and -

Two characters at the start of a recipe line change how Make runs it. I put a pair of small rules in a separate file, prefix.mk, so they don't clutter the main Makefile. make -f reads a Makefile with a different name:

.PHONY: banner tidy
 
banner:
	echo "Building logstat"
	@echo "Building logstat"
 
tidy:
	-rm logstat *.o
	@echo "tidy finished"

@ silences the echo. Normally Make prints each command before running it. With @ it just runs it:

make -f prefix.mk banner
echo "Building logstat"
Building logstat
Building logstat

The first recipe line printed the command and then its output; the @ line printed only the output. Use @ for echo lines and other noise. Don't hide real build commands with it: when a build breaks, the echoed command is the first thing you need to see.

- ignores a failure. Normally, if any recipe line exits with a non-zero status, Make stops. With - it reports the failure and carries on. Run tidy when there's nothing left to delete, for example straight after a make clean:

make -f prefix.mk tidy
rm logstat *.o
rm: cannot remove 'logstat': No such file or directory
rm: cannot remove '*.o': No such file or directory
make: [prefix.mk:8: tidy] Error 1 (ignored)
tidy finished

There was nothing to delete, rm failed, Make said (ignored) and ran the next line. My opinion: - is rarely the right tool. Here, rm -f does the job properly. It succeeds quietly when the files are already gone, without teaching readers to ignore errors. Save - for commands whose failure genuinely doesn't matter, like stopping a service that might not be running.

Each recipe line runs in its own shell

This one bites everyone. Make starts a new shell for every recipe line. Anything one line changes in its shell (the current directory, a shell variable) is gone by the next line. Here is a packaging target appended to the Makefile, written the way you'd type it at a prompt:

dist: logstat
	mkdir -p dist/logstat-1.0
	cp logstat README.md dist/logstat-1.0/
	cd dist
	tar -czf logstat-1.0.tar.gz logstat-1.0
make dist
mkdir -p dist/logstat-1.0
cp logstat README.md dist/logstat-1.0/
cd dist
tar -czf logstat-1.0.tar.gz logstat-1.0
tar: logstat-1.0: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
make: *** [Makefile:23: dist] Error 2

The cd dist worked, in a shell that exited immediately. tar ran in a fresh shell back in the project directory, where there is no logstat-1.0. Worse, it still left an empty, broken logstat-1.0.tar.gz in the project root. Two correct ways to write it:

  • Join the commands into one shell line: cd dist && tar -czf logstat-1.0.tar.gz logstat-1.0. && also means tar won't run if the cd fails.
  • Avoid the cd entirely: tar -C dist -czf dist/logstat-1.0.tar.gz logstat-1.0, where -C tells tar which directory to work in.

Long shell commands can be split across lines with a trailing backslash. Make passes them to a single shell. GNU Make also has a .ONESHELL special target that runs a whole recipe in one shell, but it changes the behaviour of every recipe in the file, so I'd use && and backslashes until you have a specific reason not to.

Dry runs with make -n

make -n (or --dry-run) prints the commands Make would run, without running them. It's the safest way to answer "what will this rebuild?" or "what does this target actually do?":

touch parse.c
make -n
ls -l --time-style=+%H:%M:%S parse.o
gcc -Wall -Wextra -c parse.c
gcc -o logstat main.o parse.o report.o
-rw-r--r-- 1 sid sid 1600 21:55:37 parse.o

Make listed the compile and the link, and parse.o wasn't touched. It works on any target: make -n clean shows exactly what clean would delete before you let it. A dry run also prints @ lines, since the point is to show everything:

make -n -f prefix.mk banner
echo "Building logstat"
echo "Building logstat"

Two related flags: make -B rebuilds everything regardless of timestamps, and make -C some/dir runs Make in another directory as if you'd cd'd there first.

Where you'll use this

Put it all together and you have the shape of most small C projects' Makefiles. This one adds two targets that real projects have: check, which runs the program on a test input and compares the output with what's expected, and a dist that works.

.PHONY: all check dist clean
 
all: logstat
 
logstat: main.o parse.o report.o
	gcc -o logstat main.o parse.o report.o
 
main.o: main.c parse.h report.h
	gcc -Wall -Wextra -c main.c
 
parse.o: parse.c parse.h
	gcc -Wall -Wextra -c parse.c
 
report.o: report.c report.h parse.h
	gcc -Wall -Wextra -c report.c
 
check: logstat
	./logstat tests/access.log > tests/actual.txt
	diff -u tests/expected.txt tests/actual.txt
	@echo "check: all tests passed"
 
dist: logstat
	rm -rf dist
	mkdir -p dist/logstat-1.0
	cp logstat README.md dist/logstat-1.0/
	cd dist && tar -czf logstat-1.0.tar.gz logstat-1.0
	@echo "created dist/logstat-1.0.tar.gz"
 
clean:
	rm -f logstat *.o tests/actual.txt
	rm -rf dist

tests/expected.txt holds the known-good report. make check builds the program if needed, then tests it:

make clean
make check
rm -f logstat *.o tests/actual.txt
rm -rf dist
gcc -Wall -Wextra -c main.c
gcc -Wall -Wextra -c parse.c
gcc -Wall -Wextra -c report.c
gcc -o logstat main.o parse.o report.o
./logstat tests/access.log > tests/actual.txt
diff -u tests/expected.txt tests/actual.txt
check: all tests passed

The useful part is the failure. diff exits non-zero when the files differ, which stops Make, which makes make check exit non-zero, which fails a CI job. Here I edited the expected 5xx count to simulate a regression:

make check
./logstat tests/access.log > tests/actual.txt
diff -u tests/expected.txt tests/actual.txt
--- tests/expected.txt	2026-11-09 21:55:40.722709404 -0500
+++ tests/actual.txt	2026-11-09 21:55:40.729679569 -0500
@@ -4,4 +4,4 @@
 2xx:      2
 3xx:      1
 4xx:      2
-5xx:      0
+5xx:      1
make: *** [Makefile:19: check] Error 1

The @echo "check: all tests passed" line never ran, because the line before it failed. That "stop at the first failure" behaviour is what makes Make safe to chain steps with. And the fixed dist:

make dist
tar -tzf dist/logstat-1.0.tar.gz
rm -rf dist
mkdir -p dist/logstat-1.0
cp logstat README.md dist/logstat-1.0/
cd dist && tar -czf logstat-1.0.tar.gz logstat-1.0
created dist/logstat-1.0.tar.gz
logstat-1.0/
logstat-1.0/README.md
logstat-1.0/logstat

make, make check, make dist, make clean is a convention, not a law, but it's so widespread (GNU's own coding standards use these names) that people will type them in your project without reading the Makefile. The repetition in the object rules is the next thing to go: Variables removes the duplicated compiler flags, and Pattern Rules replaces the three object rules with one.

Common mistakes

A header missing from the prerequisites

The quietest bug in hand-written Makefiles. Suppose the report.o rule forgot its headers:

report.o: report.c
	gcc -Wall -Wextra -c report.c

Change the title that report.c prints, which lives in report.h:

sed -i 's/"logstat report"/"access log summary"/' report.h
make
./logstat access.log | head -n 1
gcc -Wall -Wextra -c main.c
gcc -o logstat main.o parse.o report.o
logstat report

main.o lists report.h, so it recompiled and the program was relinked. But report.o, the object that actually uses REPORT_TITLE, was considered up to date. The build "succeeded" and the program still prints the old title. With a struct layout change instead of a string, this is how you get memory corruption that disappears after make clean. Put the headers back and the next make rebuilds the right object:

gcc -Wall -Wextra -c report.c
gcc -o logstat main.o parse.o report.o
access log summary

If a bug vanishes after make clean, suspect a missing prerequisite.

"No rule to make target ..., needed by ..."

A typo in a prerequisite name:

grep '^report.o' Makefile
make
report.o: report.c repot.h parse.h
make: *** No rule to make target 'repot.h', needed by 'report.o'.  Stop.

repot.h doesn't exist and no rule creates it. The message names the missing file and who wanted it. Check the spelling, and check that the file exists relative to the directory where you ran make.

Reading a compiler failure

When a recipe command fails, Make prints the command's own error, then one line of its own:

gcc -Wall -Wextra -c parse.c
parse.c: In function ‘parse_line’:
parse.c:8:18: error: expected ‘;’ before ‘}’ token
    8 |     return n == 3
      |                  ^
      |                  ;
    9 | }
      | ~
make: *** [Makefile:12: parse.o] Error 1

Read the make: *** line as: "the recipe at line 12 of the Makefile, building parse.o, exited with status 1". The real error is always above it, from the command that failed. Make stops at the first failure, and everything it built before that is still valid, so after fixing parse.c the next make picks up where it left off.

The cd that didn't stick

Covered above, but it belongs on this list: cd somewhere on its own recipe line has no effect on the next line. Use cd somewhere && command, or a tool option like tar -C.

Summary

  • A rule is target: prerequisites followed by recipe lines that each start with a TAB. Spaces instead of a TAB give missing separator.
  • make with no arguments builds the first target in the file, so put an all rule first.
  • A target is rebuilt when it's missing or older than any prerequisite. List the headers each object includes, or edits to them won't trigger rebuilds.
  • Compile each .c file to a .o with gcc -c and link the objects. Then a change to one file costs one compile and a link.
  • Declare action targets with .PHONY: all clean check dist so a stray file named clean can't stop them running.
  • @ hides the echoed command; - ignores a failing command. Prefer rm -f over -rm.
  • Every recipe line runs in a new shell. Join dependent commands with &&.
  • make -n prints what would run without running it; make -B rebuilds everything; make -f file reads a different Makefile; make -C dir runs in another directory.

Exercises

Exercise 1: Add a run targeteasy

Add a run target that builds logstat if needed and then runs it on access.log. Make sure it still works if a file called run ever appears. Test it from a clean tree.

Show Solution

Add this to the Makefile:

.PHONY: run
run: logstat
	./logstat access.log
make clean
make run
rm -f logstat *.o
gcc -Wall -Wextra -c main.c
gcc -Wall -Wextra -c parse.c
gcc -Wall -Wextra -c report.c
gcc -o logstat main.o parse.o report.o
./logstat access.log
logstat report
requests: 6
bytes:    6404
2xx:      2
3xx:      1
4xx:      2
5xx:      1

Because run depends on logstat, Make builds the program first when it's missing and skips straight to running it when it's up to date. A project can have several .PHONY lines; they add up.

Exercise 2: Add a new source filemedium

Add a module that prints byte counts in a human-friendly way. Create fmt.h and fmt.c:

/* fmt.h */
#ifndef FMT_H
#define FMT_H
 
#include <stddef.h>
 
/* Write a byte count like "5.0 KiB" into buf. */
void human_bytes(long bytes, char *buf, size_t size);
 
#endif
/* fmt.c */
#include <stdio.h>
#include "fmt.h"
 
void human_bytes(long bytes, char *buf, size_t size)
{
    if (bytes < 1024)
        snprintf(buf, size, "%ld B", bytes);
    else
        snprintf(buf, size, "%.1f KiB", bytes / 1024.0);
}

Then change report.c to #include "fmt.h" and print s->bytes through human_bytes. Update the Makefile so the program builds, and so that touching fmt.h rebuilds exactly the objects that include it.

Hint

You need a new object rule, a change to the link rule, and one more prerequisite on an existing rule. Which .c files include fmt.h?

Show Solution
.PHONY: all clean
 
all: logstat
 
logstat: main.o parse.o report.o fmt.o
	gcc -o logstat main.o parse.o report.o fmt.o
 
main.o: main.c parse.h report.h
	gcc -Wall -Wextra -c main.c
 
parse.o: parse.c parse.h
	gcc -Wall -Wextra -c parse.c
 
report.o: report.c report.h parse.h fmt.h
	gcc -Wall -Wextra -c report.c
 
fmt.o: fmt.c fmt.h
	gcc -Wall -Wextra -c fmt.c
 
clean:
	rm -f logstat *.o
make clean
make
./logstat access.log
touch fmt.h
make
rm -f logstat *.o
gcc -Wall -Wextra -c main.c
gcc -Wall -Wextra -c parse.c
gcc -Wall -Wextra -c report.c
gcc -Wall -Wextra -c fmt.c
gcc -o logstat main.o parse.o report.o fmt.o
logstat report
requests: 6
bytes:    6.3 KiB
2xx:      2
3xx:      1
4xx:      2
5xx:      1
gcc -Wall -Wextra -c report.c
gcc -Wall -Wextra -c fmt.c
gcc -o logstat main.o parse.o report.o fmt.o

fmt.h is included by fmt.c and report.c, so touching it rebuilt exactly those two objects. main.o and parse.o were left alone. If you forget fmt.o in the link rule, the link fails with undefined reference to 'human_bytes'.

Exercise 3: Fix the dist target without cdmedium

Rewrite the broken dist target from the "own shell" section so it works without any cd at all, and so it starts from an empty dist/ directory each time. Remember to declare it phony.

Show Solution
dist: logstat
	rm -rf dist
	mkdir -p dist/logstat-1.0
	cp logstat README.md dist/logstat-1.0/
	tar -C dist -czf dist/logstat-1.0.tar.gz logstat-1.0

and change the .PHONY line to .PHONY: all clean dist. Then, from a clean tree:

make clean
make dist
tar -tzf dist/logstat-1.0.tar.gz
rm -f logstat *.o
gcc -Wall -Wextra -c main.c
gcc -Wall -Wextra -c parse.c
gcc -Wall -Wextra -c report.c
gcc -o logstat main.o parse.o report.o
rm -rf dist
mkdir -p dist/logstat-1.0
cp logstat README.md dist/logstat-1.0/
tar -C dist -czf dist/logstat-1.0.tar.gz logstat-1.0
logstat-1.0/
logstat-1.0/README.md
logstat-1.0/logstat

tar -C dist changes directory inside tar itself, so it doesn't matter that each recipe line has its own shell. The output path dist/logstat-1.0.tar.gz is resolved before the -C takes effect, which is why it still includes dist/. Phony matters here for real: the recipe creates a directory named dist, and without .PHONY the second make dist would say 'dist' is up to date.

Exercise 4: Predict a header changehard

With everything built, you're about to touch parse.h. Using the include table at the top of this article, predict exactly which commands Make will run and in what order. Check your prediction with a dry run before running the real build.

Show Solution
touch parse.h
make -n
gcc -Wall -Wextra -c main.c
gcc -Wall -Wextra -c parse.c
gcc -Wall -Wextra -c report.c
gcc -o logstat main.o parse.o report.o

All three objects: main.c and parse.c include parse.h directly, and report.c includes it through report.h, which is why report.o's rule lists parse.h too. Then the link. The order follows the prerequisite list of logstat (main.o parse.o report.o), left to right. make -n ran nothing, so a real make afterwards prints the same four commands.


Tested with: GNU Make 4.3 and GCC 13.3 on Ubuntu 24.04 (WSL2), November 2026. Every Makefile, command and output above was run as shown, in a fresh copy of the project for each section. The one trim is the sample log, shortened with […] after three of its six lines.

Was this page helpful?
The Dark Debugger

Written by The Dark Debugger

Engineer documenting the things that took me too long to learn, so they take you less time. Every Makefile tutorial here is tested end to end before publishing.