A single Makefile can build an entire codebase, but only if it encodes the full build graph in its rules: which source files produce which objects, how those objects link into libraries or executables, and which steps generate code or assets along the way. The "single file" part is easy; the real requirement is that the one file declare every dependency the build needs. Most confusion around this topic comes from treating a Makefile as a list of commands rather than as a dependency description — and from underestimating how much can go wrong once a project grows past a few source files.

The question behind the search phrase is usually one of two things: "Is it possible to manage a whole project's build from one Makefile instead of a build system like CMake or a task runner like npm scripts?" or "How do I write that Makefile?" The answer differs slightly by language, but the principles are the same, and the limits are the same. This article covers both: what a single Makefile can genuinely accomplish, the patterns that keep it maintainable, and where it stops being the right tool.

What "one Makefile" actually means

GNU Make reads a file — by default named Makefile, makefile, or GNUmakefile — that consists of rules, variable assignments, and directives. A rule has the form:

The target is a file to produce (or a name like clean that never corresponds to a file). The prerequisites are the files or other targets it depends on. The recipe is the command sequence, and each recipe line must begin with a literal tab character — a detail that trips up more newcomers than any other.

Make's core algorithm is simple and worth stating plainly because everything else follows from it: for each requested target, Make checks whether the target exists and whether any prerequisite is newer than it. If the target is missing or out of date, Make runs the recipe, first recursively ensuring each prerequisite is up to date. That timestamp comparison is the entire reason Make exists: it rebuilds only what changed, and only what depends on what changed.

So a "single Makefile to build the code" is not a script that runs every compiler command in order. It is a declarative map of the dependency graph. If the map is accurate, the build is correct and incremental. If the map is incomplete, the build is fast and wrong.

The minimal single Makefile that is actually correct

Here is a small but honest C example, which generalizes well to C++ and to many other compiled languages:

Several things in those ~25 lines carry most of the weight:

$(wildcard ...) plus substitution derives objects from sources, so adding a file requires no edit. The substitution reference $(SRCS:src/%.c=build/%.o) is the idiomatic way to remap a path. -MMD -MP makes the compiler emit a .d file listing every header each object includes; -MP adds phony targets so deleting a header does not break the build. -include $(DEPS) pulls those files in silently. Without this, header changes do not trigger recompilation — the single most common correctness bug in hand-written Makefiles. Order-only prerequisites (| build) ensure the output directory exists without treating its timestamp as a reason to rebuild every object. $@, $^, $< are automatic variables for the target, all prerequisites, and the first prerequisite respectively. Using them keeps recipes short and avoids duplicating filenames, which is how Makefiles rot. .PHONY marks names like all and clean so they run even if a file with that name exists.

This pattern scales to dozens of translation units without change. For C++, swap CC for CXX and adjust flags; the dependency generation works identically.

Multiple programs, libraries, and code generation in the same file

Real projects rarely produce exactly one binary. A single Makefile can still hold the whole build if you make the structure explicit rather than hand-writing a rule per file.

The important decision is to generate object lists from source lists per component, then write one link rule per binary. That keeps the file long but linear: adding a module means adding a source file, not editing rules.

Code generation — parsers, protocol definitions, embedded assets — fits the same model as long as you express it as ordinary rules:

Two pitfalls matter here. First, $(@D) (the target's directory) handles creation of intermediate directories. Second, generated sources must appear in the prerequisite position of whatever compiles them, not just in the source list, or Make may compile stale output. The safest pattern is to list generated files as explicit prerequisites of the objects that need them, which makes the ordering automatic.

Parallelism, and why dependency accuracy is now a safety issue

Running make -j8 executes independent recipes concurrently. This is where a single well-specified Makefile earns its keep: the dependency graph, not the recipe order, determines sequencing, so make -j is safe as long as every real dependency is declared.

It is also where sloppy Makefiles fail, sometimes intermittently. If an object silently depends on a generated header that is not listed as a prerequisite, a serial build happens to work and a parallel build fails at random. The fix is always the same: declare the dependency (or generate it with -MMD). A Makefile that is only correct with -j1 has an incomplete graph.

Where the single-file approach strains

A single Makefile is not the same as no structure, but it does mean everything lives in one namespace. Problems typically appear in these areas:

Directory-level structure. In a multi-directory project, recursive Make (make -C subdir) is traditionally used, and it has known costs: it fragments the dependency graph, so the top-level make cannot parallelize across directories accurately and rebuild decisions can miss cross-directory dependencies. The alternative is the "single top-level Makefile that names all paths" — the subject of this article — which is generally more correct but requires discipline about path variables.

Configuration and portability. Builds that must detect compilers, check for libraries, and adapt flags benefit from a configuration step. Autotools and CMake exist because hand-writing that logic in portable Make syntax is laborious. A single Makefile can still be portable by delegating detection to small shell probes, but it is doing manually what those systems automate.

Scale and discoverability. Thousands of files and dozens of components push toward generated makefiles or a higher-level system. The switch point is not a fixed number; it is the point where a maintainer can no longer see the whole graph in their head or find the rule that produces a given artifact.

Non-C toolchains. For Go, the toolchain itself resolves packages, so a thin Makefile wrapping go build is normal and the dependency tracking Make offers is largely redundant. For JavaScript and TypeScript, make can serve as an orchestrator over npm/tsc scripts, but module-level dependency management lives elsewhere. For Java, javac has partial dependency tracking and build tools like Maven or Gradle handle the rest. The single-Makefile pattern is strongest where the compiler does not manage dependencies itself.

Non-compilation tasks. Test runs, lint, formatting, packaging, and container builds fit fine as .PHONY targets. That is a legitimate use of a single Makefile as a project command surface, and many teams keep exactly that even when a separate build system produces the binaries. Mixing the two roles in one file is fine if the phony targets are clearly marked.

Common failure modes, and the tests that catch them

Most complaints about broken Makefiles trace to a short list:

1: Spaces instead of tabs — Make reports "missing separator." 2: Missing header dependencies — stale objects after changing a header. Test: touch a_header.h && make; every dependent object should rebuild. 3: Glob results frozen at parse time — $(wildcard) is evaluated when Make reads the file, so files created by an earlier rule in the same run are not picked up. Re-running make fixes it, but the graph is subtly wrong. 4: VPATH surprises — using VPATH to find sources while writing outputs in-tree creates confusing collisions. Prefer explicit paths in source lists and separate output directories. 5: Recipes that ignore failures — make stops at the first failing command, which is usually right; do not prefix with - unless failure is genuinely acceptable. 6: clean that misses something — deriving the object list from sources and removing the derived directories is more reliable than listing files by hand. 7: Phony targets colliding with files — forgetting .PHONY gives confusing "up to date" messages.

A good self-check is to delete the build directory and rebuild from scratch, then touch a header and rebuild, then run with -j and compare. If all three produce identical, correct results, the single Makefile is trustworthy.

When to keep one file and when to split

The practical guidance is straightforward. Keep a single Makefile when the project is one codebase with a shared dependency graph, when paths can be named from the top level, and when the compiler does not already handle dependency resolution. Split, or adopt a generated build system, when the project spans multiple independent components with their own toolchains, when configuration logic becomes substantial, or when the top-level file's length makes the graph hard to audit.

If the one-file approach is chosen, treat the file as a graph description with a small amount of structure around it: grouped variable definitions for paths and flags, pattern rules for compilation, explicit link rules per artifact, generated-dependency includes, and phony targets for everything that is not a file. That division keeps a single Makefile legible at a few hundred lines, which is where most projects that ask this question actually sit.

Do not mistake "one Makefile" for "one rule." The power of Make is that a handful of general rules plus accurate source lists can describe an arbitrarily large build; the failure mode is hand-maintaining dozens of nearly identical rules. Write the general rule, let the lists do the enumerating, and let the compiler emit the dependency data. That, more than any particular syntax, is what makes a single-file build both possible and reliable.