r/cpp • • 25d ago

The case against makefiles

https://thibaud.space/2026/10/the-case-against-makefiles.html
0 Upvotes

55 comments sorted by

8

u/PseudoFrequency 25d ago

Tell me you have absolutely no idea what make is for without telling me. Hint: it's not even remotely for the same use case as shell scripts.

2

u/com_kieffer 24d ago

I fear that you may hae missed the argument i was trying to make.

If you read the article, you'll notice that I am not advocating for shell scripts as a replacement for makefiles.

14

u/holyblackcat 24d ago

Your bash script is missing:

  1. The ability to rebuild only the files that have changed since the last build, or that include headers that have changed.
  2. Parallel builds.

Both are trivial in Make or in any serious build system. You'll need some horrible bash wankery to implement those.

1

u/com_kieffer 24d ago

That's exactly the point, the sh script is the predecessor to makefiles. I might not have been clear enough, but it's only there to showcase the reasons that makefiles were created.

8

u/baudvine 25d ago

An equivalent makefile would be

an equivalent Makefile would be almost empty because Make ships with those rules. Read the dang manual.

(that said, I mainly use CMake and as of today "just" for the command running case. Still, people treating Make as a command runner (and that includes the just docs) bug me to no end.)

2

u/smdowney WG21, Text/Unicode SG, optional<T&> 25d ago

I use make to drive the cmake workflow, I want β€œtest” but if the build has to run, do that, and configure if that has to.

Makefile is a language for writing rule based expert systems, and it is really good at that stuff.

0

u/com_kieffer 24d ago

The makefile might be a bit redundant, but the example would be a lot less interesting with an empty makefile.

22

u/goatshriek 25d ago

make's ability to avoid rebuilding something that is already done seems like it should be at least mentioned here. The bash script example given builds everything every time, but the equivalent makefile will only build things that have changed, which could make a significant difference in a larger project, irrespective of parallelization.

8

u/OrphisFlo I like build tools 25d ago

It's unfortunately too simplistic as make will only rebuild when some of the inputs of changed. The most important input is always forgotten in those sort of build files: the build files themselves.

If you change the build actions, nothing is rebuilt. And your tool will say that everything is up to date, leading to a wrong build.

The only solution in those cases is to clean and rebuild, and you lose all the benefits.

CMake will handle that correctly and is able to write Makefiles that will handle this. But it's just a lot of extra work that the tool should have handled from the start, like Ninja is doing.

Most Makefiles written by hand are not correct. Do not write Makefiles unless you are doing something extremely simple or you are an expert (even then, you'll know there are better tools...).

9

u/Lost-In-Void-99 24d ago

This is because the dependency graph does not include build script, not because it cannot be done. Some people are just too lazy to read manuals.

1

u/holyblackcat 24d ago

I don't want it to track the entire makefile, though. I only want to track the command line of the file I'm building.

I guess you can move anything accepting flags to a separate makefile included in the main one, but it's still quite annoying to do.

2

u/johannes1971 23d ago

Similarly, I don't want to build my entire translation unit, I only want to build the function that I changed. But somewhere along the way we apparently decided that 'files' were the unit in which we decide everything.

It doesn't have to be: C++ could be stored in a database (or graph, if you prefer), with each entity linked to the things it consumes, and the things that consume it. That would allow for selective recompilation instead of having to recompile half your project when you change one constant in a header, work a lot faster (no more duplicate template instantiation), and provide the benefits of full program optimisation/analysis as its basic mode of operation. We're firmly stuck in file-thinking, though...

4

u/OrphisFlo I like build tools 23d ago

I believe that's what zapcc tried to do a while back.

2

u/johannes1971 23d ago

_Did_. Not "tried to do".

4

u/OrphisFlo I like build tools 23d ago

The long version would be "tried to do at scale in a commercial compiler very few people used before they went under and abandoned it".

Sure the tech worked, but if you don't upstream your clang patches, you will always have a painful catch-up phase and people will likely not use your program. Which isn't great for a commercial product!

→ More replies (2)

2

u/Circlejerker_ 23d ago

Imagine having to manage includes/imports for individual functions/classes/objects.. And then the build system would have to diff files to figure out which functions changed within a file.. It for sure is an interesting thought, but it does not seem practical.

→ More replies (1)

1

u/OrphisFlo I like build tools 24d ago

If you add the Makefile as a dependency, a change to a target A will invalidate the target B too. It's unacceptable at scale.

1

u/arthurno1 24d ago

The only solution in those cases is to clean and rebuild, and you lose all the benefits.

So it is with other build tools and scripts as well. If you change your build script you are probably changing it because you wish a different output, no?

Typically also you are fixing your build script once and calling it many times.

3

u/OrphisFlo I like build tools 24d ago

No, modern build tools will record the command line that generated a target and automatically regenerate it if it's different.

Make will just tell you there's no work to be done. So in an automated system (for example CI, or even a local developer), you can't just do an incremental build. You need extra logic to clean if any build file changes, and that has to be done manually, it's error prone. So you learn not to do incremental builds after a git pull or to never trust your build system at all.

1

u/JMBourguet 24d ago

From Ninja doc:

Outputs implicitly depend on the command line that was used to generate them, which means that changing e.g. compilation flags will cause the outputs to rebuild.

4

u/OrphisFlo I like build tools 24d ago

Exactly, it's stored in the .ninja_* files that are created. Ninja is very much a modern build tool that I referred to above (even though I'd like to see a lot more in there...). Make is not modern.

5

u/JMBourguet 24d ago

Oops, I read "No modern build tools will" where you wrote "No, modern build tools will".

5

u/OrphisFlo I like build tools 24d ago

I see, no worries!

1

u/evaned 24d ago

Most Makefiles written by hand are not correct.

I would go much farther than most, I suspect it is the vast, vast majority.

And that's if you accept that it's even possible to be "correct" with timestamp-based decisions, something I'm not sure I accept. Then again, it's not like there's much choice in the matter; I'm aware of only a couple build systems that do something better, and they're... not popular.

1

u/OrphisFlo I like build tools 24d ago

They are not popular on the surface for open source, but in large companies, Bazel is becoming a must to get speed, correctness and save a lot of the CI budget.

AI driven usage has multiplied the number of builds a lot, which traditional build systems won't handle well and just scale the cost linearly unfortunately. With Bazel correctly deployed, you can save a lot.

And with AI migrations being easy enough to do without having the need for years of Bazel expertise to drive it, it's becoming a real opportunity now.

I bet that with the right infrastructure available, Bazel would also be quite popular for OSS projects. Imagine if you had a caching server available directly for your builds that doesn't require to download and upload a huge archive ahead of time on GitHub. Most builds would be so much faster without doing anything and it's save on a lot of CPU usage for the global GitHub fleet...

1

u/wlandry 23d ago

As someone who has to deal with Bazel every day at $MEGACORP, almost everything you wrote is wrong. Bazel does not give you speed, correctness, OR save the CI budget. Hermeticity is a lie.

It does make things anti-portable, fragile, and impossible to upgrade. Companies ignore these problems, but OSS can not.

I will agree with you that Bazel is becoming more popular at companies. It seems to allow them to lean in on bad practices. It makes rebuilding the operating system with every PR possible for the low, low cost of millions of dollars. The results are usually the same.

→ More replies (4)

1

u/RogerV 24d ago

just have AI author and maintain makefiles

1

u/victotronics 24d ago

"make's ability to avoid rebuilding something that is already done" True, but cmake generates makefiles that do this correctly. So you don't have to write makefiles explicitly, you let them be generated by cmake. And I'm assuming that cmake generates similar files if you use Ninja instead of Make as the backend.

14

u/Karr0k 25d ago

Ok, now make it cross platform, like those 3 lines of cmake provide, and support multiple toolchains.

git outa ere with that archaic bash ..

2

u/ArodPonyboy 25d ago

Why not meson?

3

u/com_kieffer 24d ago

I added a meson example just for you!

2

u/ArodPonyboy 24d ago

How thoughtful! I love it 😁

11

u/LeeHide just write it from scratch 25d ago

so just is slightly better than make? why not just use cmake?

2

u/shahms 25d ago

Because CMake is just slightly better than autotools

5

u/smdowney WG21, Text/Unicode SG, optional<T&> 25d ago

That really is its niche, and why it writes makefile, or ninja, for the actual running of stuff.

1

u/forknull 20d ago

I think you are (badly) missing the point. Don't feel bad, it happens to all programmers who get in the craft, about halfway through (the "expert" valley).

Make is a general-purpose dependency-driven execution engine. CMake and Meson are build-system generators or, if you will, higher-level build descriptions. They operate at different levels of abstraction.

Make's central proposition isn't β€œI know how to build C programs.” which is what you are after. It's something closer to:

When given a set of targets and their prerequisites, and the rules for producing them, determine what needs to happen, in what order, and execute only the necessary operations.

That is as general and as powerful an abstraction as you will ever encounter. YOu are bound to miss it, and this is relevant, because you don't get that the edges are important because they describe relationships and constratints without the need for semnatic knowledge.

2

u/com_kieffer 19d ago

I don't think that we are saying different things.

In my bubble (embedded software), make is often used as the build system. The build is a hand-crafted makefile, written by somebody who may, or may not have, at some point read the manual for make. As a result the makefiles are cryptic and barely work (seriously, parallel builds reliably fail).

The makefile as an output of a meta-build system (Cmake of meson) is fine. I don't have to read it, it just builds and executes the graph. For that task though, ninja is bounds ahead.

1

u/forknull 18d ago

If I had a nickel for every time I heard exactly what you are saying. Unfortunately, there is a certain kind of people who don't know the tool but are still compelled to write nonsensical essays on how the tool is bad.

3

u/Cross_22 25d ago

In my opinion the two issues with all those tools is that a) they use domain specific language and b) care more about being generalizable to edge cases instead of easily covering the most prevalent use cases.

If there's a dedicated build engineer who handles this then I don't care what's being used, but if I have to learn a complex system just so I can compile and link my sources then we are already off to a bad start.

2

u/victotronics 25d ago

I use Make often as a task-runner. For instance, I have a makefile for complicated LaTeX documents (does CMake does that?), and when the formatting is successful I copy the result somewhere and do a git push with it. Kindly sketch me how to do this with cmake and I might be tempted to switch.

Makefiles require some discipline, but I am disciplined, as experience has taught me to be. So next to each major target in the makefile I have a "info::" rule that prints what target can be made, and with what parameters. Can cmake document its own workings? For instance can I have cmake print out all its targets in a CMakeLists, recursively of course? Can it spit out all the SET parameters that I can "-D" on the commandline? (And don't say ccmake because that only has that information after you've done a successful cmake run. Circularity hello.)

2

u/serviscope_minor 24d ago

I use Make often as a task-runner. For instance, I have a makefile for complicated LaTeX documents (does CMake does that?),

It can but at that point you find out that advantage of CMake in this case is the "outcome" based thing works only because it has a bunch of stuff built in. Once CMake doesn't have in-built graph edges then you have to tell CMake what they are, just like make.

But also I believe cmake does way less inference, and you can't give it edge patterns: it is much more procedural than make. So if you have something like:

final.pdf: a.tex b.pdf c.pdf d.pdf

and %.pdf: %.svg

and d.svg: d.py

it won't work out by itself that it can resolve it all by generating d.svg from d.py, then turn all the SVGs into PDFs then compile the latex file into a PDF.

I think in CMAke, you need to give it the individual edges, you can't have it infer them from the patterns and your directory tree.

ETA: if I'm compiling portable C++, CMake is my choice because it's cross platform and includes configuration and includes a ton of useful C++ building related stuff.

If I'm doing my own non C++ stuff on Linux, then gnu Make is a considerably better choice.

1

u/arthurno1 24d ago

The same here. I write mostly Lisp nowadays, which comes with its own build system that actually understands Lisp. However I still use makefiles as a task runner. I could certainly use shell scripts and use functions or whatever; but why? I would have to type lots of conditionals and helepr functions to achieve what Make does with few lines.

-1

u/Frexxia 24d ago

For instance, I have a makefile for complicated LaTeX documents

Why not just use https://ctan.org/pkg/latexmk?lang=en ?

3

u/victotronics 24d ago

"Just". Did I not say "complicated"? My makefile starts by rsync'ing files from multiple locations, running a bunch of unix scripts, then formatting, then copying to other locations and "git add"ing the results.

1

u/Frexxia 24d ago

Fair enough, though that sounds like complexity for the sake of complexity

2

u/victotronics 24d ago

No. That's complexity for the sake of having more than 2000 pages of 5 volumes of textbooks, each using 100s of programming examples, and trying to use as much common code & macros as possible.

1

u/smdowney WG21, Text/Unicode SG, optional<T&> 23d ago

Because that's what the Makefile ends up invoking? But there's more steps before and after, dependency tracking, etc

https://github.com/bemanproject/optional/blob/main/papers%2FP2988%2FMakefile

1

u/vI--_--Iv 20d ago

you forgot to change your editor settings to use tabs for indentation instead of spaces

... N years ago.
This is the only way.