8 ms·
Using Make – writing less Makefile
- Augenstern 3y agoI like the man page format each post has on this website. A little hard to read, but very unique.
- edoardo-schnell 3y agoYeah, 2023 and reflowing documents still isn't a thing. Yay
- Conscat 3y agoI read this on mobile and it felt pretty tedious. I wonder if maybe some minimal HTML could make the paragraphs reflow properly while still looking like a manpage everywhere.
- 082349872349872 3y agoWhat sort of keyhole are you peering through that needs to reflow 72-character text lines?
- plugin-baby 3y agoA phone? Your comment is 3 lines for me.
- 082349872349872 3y agoJust checked; both my comment and TFA appear full width on my phone. Bog-standard Android.
- dxdm 3y agoMaybe other people do not use the same setup as you do.
- 082349872349872 3y agoThat much is obvious given that a vanilla phone setup has no trouble with even 90-character lines. Not my business to stop anyone making things hard on themselves, though, so good luck you all! (do we have anyone who reads HN over a morse clacker?)
- pests 3y agoI have vision problems, I increase zoom a lot in desktop and mobile. What do you have against people different than youself?
- 082349872349872 3y agoNothing; I wished you luck! I imagine people with screenreaders have an even tougher time... (mobile I agree would be hopeless with zoom [I avoid reading on mine even without zoom], but does your desktop zoom mean even TFA's vt100-friendly 72-chars needed reflowing?)
- pests 3y agoI don't always increase zoom but TFA is def too small on desktop. I just increase until its readable, that being said, the article is comfortable for me at about 200-250%. Reflow issues seem to occur at 300% - so no issue for me but I could see some people going that high.
- Karellen 3y agoWhich is ironic, given that man pages do reflow on different-width terminals and have done since the '90s.
- esafak 3y agoThis is supposed to be functional, not art work. Make it legible; easy to digest. I guess it's fitting for a tool (make) that is also unergonomic by today's standards.
- Augenstern 3y agoI wouldn't use this format myself either, but that's the fun with personal websites: you can do whatever you want. There's no law that says stuff needs to be functional. People who dislike it will just move on.
- mosselman 3y agoTypography isn’t set. It _is_ about legibility and therefore proper typography _is_ functional. This site simply isn’t as legible as it could be. I am not suggesting it needs to be “pretty”. A monospaced Markdown file would be more legible.
- bsdpufferfish 3y agoIt's unfortunate that most programmers only experience with make is automake/autoconf. This is fantastic.
- NelsonMinar 3y agoAutoconf, that's the thing that spends time making sure that my software can be built on BeOS and A/UX?
- asveikau 3y agoMore like some obscure pre-c89 stuff. But it takes actual work and testing to get that working, so most people have a configure script that checks for all that stuff but when it's time to compile it only actually works on their specific Linux distro.
- im3w1l 3y agoI have a vivid memory of autoconf. I set it up best as I could. Someone ran ./configure. It triggered a new run of autoconf that failed because of mismatching autoconf version. Why it triggered a new run? Because of mtime checks. Why did they say it needed to be rerun? Because git doesn't preserve mtime. I guess it's just assumed that everyone distributes their sources as a tarball. I spent many hours trying to figure out how to bypass those issues, and I can't remember if I ever succeeded. These days I just use Rust instead.
- dgfitz 3y ago> I spent many hours trying to figure out how to bypass those issues, and I can't remember if I ever succeeded. Might I suggest “got-restore-mtime” for this, upstream in Debian and Ubuntu already. Did you really spend hours on this?
- e12e 3y agoThis? https://manpages.org/git-restore-mtime https://manpages.org/git-restore-mtime
- raggi 3y agoIf you want to write less makefile, you don't need to define foo. `foo` is an implicit target if foo.c is present. You can use make in this form without a makefile even: ~ % echo '#include <stdio.h>\nvoid main() { printf("OHAI\\n"); }' > ohai.c ~ % make CFLAGS=-DDEFINE_ME_A_MAKEFILE ohai cc -DDEFINE_ME_A_MAKEFILE ohai.c -o ohai ~ % ./ohai OHAI
- assbuttbuttass 3y agoFor larger programs, the compiler can also generate the dependency tree for incremental compilation: https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html https://www.gnu.org/software/make/manual/html_node/Automatic...
- gumby 3y agoWith gnu make you don't even need OBJs for a simple directory in which all the .c files are compiled: .SECONDEXPANSION foo: $$(patsubst %.c,%.o,$$(wildcard *.c)) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ Admittedly you can end up with short, very general Makefiles that look like they were a Prolog program written in TECO. But the advantage is that they are general so don't need to be fiddled with as your project grows. I make small Makefiles like this that ensure my local build environment is correct for the package, then call cmake.
- JadeNB 3y ago> a Prolog program written in TECO I know we don't do general-purpose "I like this" comments on HN, but, as a Christmas treat to myself, I'm just going to say how happy this little sentence fragment makes me.
- gumby 3y agoThanks! I aim to please. As it happens I used to program in both Prolog and TECO, in the same period of time but of course not on the same code bases.
- bigdict 3y agoJust do: .SECONDEXPANSION foo: $$(patsubst %.c,%.o,$$(wildcard *.c)) No need to tell Make how to invoke a C compiler for the billionth time.
- rurban 3y agoBut then you need the name it GNUmakefile when you use incompatible GNU extensions. BSD makefiles do look better.
- Brian_K_White 3y agoGlobbing is possible but unwise. You do want a variable with an explicit list of sources, even while trying to make things as automatic and dynamic as possible.
- bigdict 3y agoThe article reads I think an important thing to know about make(1) is that you don't need to write a Makefile to use it. There are default rules for C, C++ and probably Fortran. And then it goes against its own spirit... You don't need this: OBJS = foo.o bar.o baz.o foo: $(OBJS) $(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@ Just do: foo: foo.o bar.o baz.o It respects LDFLAGS and LDLIBS.
- Brian_K_White 3y agoLists of files are usually used more than once for more than one task. This would only make sense if the only rule that needed the list was that one that builds, no install/uninstall/package/modify/audit/hash/clean/etc, which is never the case. You also don't want to use globbing in place of an explicit list either.
- bigdict 3y agoSure, I'm just pointing out you don't need to type out the C compiler invocation again. FOOOBJS = foo.o bar.o baz.o foo: $(FOOOBJS)
- Brian_K_White 3y agoAh got it. Agreed.
- e12e 3y agoSo: > The example Makefile in its entirety: CFLAGS += -Wall -Wextra LDLIBS = -lcurses OBJS = foo.o bar.o baz.o foo: $(OBJS) $(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@ foo.o bar.o: foo.h clean: rm -f $(OBJS) foo Become: CFLAGS += -Wall -Wextra LDLIBS = -lcurses OBJS = foo.o bar.o baz.o ? foo: $(OBJS) foo.o bar.o: foo.h clean: rm -f $(OBJS) foo
- Karellen 3y agoBut the OBJS allows for an easy `clean` target, defined further down the article. Personally, I'd have gone with foo: $(OBJS) $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) to put all flag options first, then the output option, then using `$<` for the compiler inputs, and additional linker inputs last. But that is somewhat idiosyncratic, and based on some half-remembered ideas of second-hand stories of how compilers used to process their command parameters back in the '90s (and before). Those ideas probably aren't relevant now (and may not have been entirely correct originally), so if the recipe in the original article works for the author, I'm not going to say they're "wrong" for doing it the way they did.
- yegle 3y agoThe author should mention the automatic variables: https://web.mit.edu/gnu/doc/html/make_10.html#SEC94 https://web.mit.edu/gnu/doc/html/make_10.html#SEC94 Specifically, in one of the examples, to list all dependencies as the input to `cc`, you can write `$^` instead of `$(OBJS)`.
- djbusby 3y agoOBJS is a little more clear to what's happening than $^ I'm not a fan of magic symbols. Hard to remember, especially across languages.
- slau 3y agoMake actually has some of the more sensible shortcuts, which are easy to remember $@ -> the target (like an email address) $^ -> the dependencies (look up) $< -> only the first dependency (look up sign, but rotated 90s left to point at the first one)
- gosub100 3y agoI say this as someone with 20+ years of experience in C and C++: Using make is a huge waste of time in 2023 (and forward). Learning the intricacies of make actively blocks you from Getting Things Done. Yeah, make is kind of neat because it has this functional what_I_want : how_to_get_it syntax, but it doesn't actually work that way. It's a huge waste of time to try to get it working so unless you meet the following caveats, make is not for you: - you love trivia like "who was the 19th president of the US?" - on a road trip, you stop to look at historical markers - you love steampunk/tube amplifiers/making pasta from scratch/listening on vinyl/home brewing beer If those sound like you, you're the type who won't regret wasting a couple days getting make running and getting cut on the sharp edges. Most of us who just want to get there will be better with cmake, meson, or hand-rolling a script using a modern language (inb4 "make does more than compile code" - yeah, so does ruby or python or any other script, without making false promises or wasting your time)
- mathematicaster 3y agoLost me at "making past from scratch".
- jjgreen 3y agoIn some sense, don't we all make the past from scratch? [stares into middle distance]
- spicybright 3y ago3 paragraphs of rambling for "make is a waste of time because of sharp edges" Can you actually articulate what those sharp edges are?
- gosub100 3y agoyes, I can
- t43562 3y agoIf meson fits your project it's worth using it - it could turn into a straightjacket later but changing to something else isn't the end of the world if it cost little effort to implement it in the first place. As for trying to use a "modern" language to write scripts.......well if they're not declarative and don't understand dependencies then how can they be better? make is no paragon - it just contains solutions to lots of problems that exist in build systems. The fact that those problems exist and make has ways to solve them isn't make's fault. Everyone might perhaps want builds to "just work" so they can ignore them and such people write scripts and use the concept of "rebuild and clean" a lot and it works until the build gets big and slow. "modern" tools get written with many simplifying assumptions and become slowly complicated as they hit all the problems make hit long ago and try to find ways to solve them.
- WesolyKubeczek 3y agoMake is fantastic. I’m using it to rebuild and run containers in my development environment, and to build a full repository of RPM packages (that my project needs and which CentOS Stream lacks). I suppose that if javascript tooling was more amenable to process one file at a time, I could do parallel transpiling and bundling of a hefty frontend with make as well. I also use its metaprogramming and late evaluation capabilities a lot, heavily inspired by the tricks Buildroot is doing. If you are mindful of its restrictions (it works with “words” separated by spaces, thus paths with spaces in them are a problem unfixable without severely breaking backward compatibility; and your targets need to be expressible as files with meaningful modification timestamps), it’s a very powerful tool. It deserves more appreciation.
- grungyneer 3y agoI too have found it handy for managing containers, container networks, and images. It's prevented me from using docker-compose as much as I pronanly should.
- amelius 3y agoThings get messier when you start to add automatic dependencies.
- rochak 3y agoGonna get downvoted to oblivion for saying this but I haven't hated a tool more than make.
- bxparks 3y agoNot downvoting you. Just curious to know why? In my experience, as long as the Makefile is less than about 100 lines, it's the most useful workflow system ever. Because it's available almost everywhere. After about 100 lines, yeah, it is not great. And that idiotic tab character. I know the historical reason why tab was used. I wish they had fixed it to accept both tabs and spaces a long time ago.
- nickelpro 3y agoIt's a poor, verbose, arcane solution in every problem space it purports to solve. It was fine in its era, but that's a long time ago. It is completely unsuitable as a project management tool and a poor task runner. Also its ubiquity is overhyped. There's no build platform where I can't download the tools I need, that's the entire point of a build platform, and most such platforms come with far more advanced tools pre-installed.
- t43562 3y agoMake has few dependencies so it's a way to build an operating system up from ground 0. Any tool/language that wishes to be generally useful in an OS doesn't want to be built by something that is only available much further up the tree like python. So it gets used a lot and it's only a language for dependencies and rules. It doesn't do "project management". Its the simplicity of what it's trying to that saves it from ever becoming irrelevant - because it can be made to fit almost any use case. It's not a special tool for enforcing one structure or building only one language - something that seems highly regressive to me but which is adopted by many languages now.
- nickelpro 3y ago> Any tool/language that wishes to be generally useful in an OS doesn't want to be built by something that is only available much further up the tree like python. This is a non-priority, an unreal use case. I can't build GCC without a functioning C++ compiler and a fairly sophisticated OS environment and that's fine. Real-world use cases for bootstrapping a build environment from rubbing two sticks together are so exceptionally rare as to be near fictional. Even if we allow that such cases exist, they are unicorns, not a thing to design tools around. > It's not a special tool for enforcing one structure or building only one language - something that seems highly regressive to me but which is adopted by many languages now. This is what makes it so unsuitable. A tool that can make no assumptions about its application is less and less useful a tool. A bread knife is better at cutting bread than a plain 10" kitchen knife, a boning knife better for deboning, etc. We live in a world where for every language there are build tools that know far, far more about the needs of that language environment than make, and thus are far better suited.
- masspro 3y agoQuasi-tangent: I was once hit by a coworker doing first code review on a new repo with "hmm...I'm not familiar with using Makefiles as a project management tool. So something something [don't remember] we should replace that." It struck me as weird because I don't see `make` as a build tool so much as automating shell script snippets you would/could type at the command line. From that POV I conceptually see Makefiles as like a `bin/` folder full of little scripts, honestly the main advantage for me being every command runs with working dir being where the Makefile is; no need to write a `set -euo pipefail; HERE="$(dirname "$0")"` dingleberry at the top of every hypothetical `bin/` script. And having incremental rebuild is sugar on top when it's possible to write :) I don't see `make` as a build tool because I believe from experience that is where the road to hell begins. But it is convenient to have `make` call CMake/Cargo/pip/whatever. Plain `make` can build, `make test` can test, `make fmt` can auto-format... Even better if you alias `m` to `make` in your shell.
- gregwebs 3y agoYour coworker's experience is more principled: Make is a mediocre tool for executing commands. It wasn't ever designed for that. Although it is pretty common to see what you are mentioning in projects because it doesn't require installing a dependency. For a repo where an easy to install (single binary) dependency is a non-issue, consider using just. [1] You get `just -l` where you can see all the command available, the ability to use different languages, and overall simpler command writing. [1] https://github.com/casey/just https://github.com/casey/just
- MrOxiMoron 3y agojust is great, I add it everywhere, just test, just run, just fix, just shell. just works ;-)
- masspro 3y agoThis looks really nice. But then should `j` alias to `just` or to `jobs`? Non-starter. /s
- sime2009 3y ago
- datadeft 3y agoIs there any reason to use make over ninja and gn?
- baq 3y agoYeah, if you have no effing idea what ninja or gn are
- spicybright 3y agoI'm a seasoned developer for the past 15-ish years and I haven't heard of ninja or gn before. I tried typing them in my command line (macos 12.4) and I would need to install them to use them. I was taught make in CS101 in college and the majority of other people's projects I've looked at use it. So the advantage is ubiquity and familiarity. Which can be useful if your code base is going to have a large number of people working on it.
- datadeft 3y agoNinja was super easy to pick up even after using make for some time (10+ years). GN is just a ninja generator that is optional. https://gn.googlesource.com/gn/+/main/docs/quick_start.md https://gn.googlesource.com/gn/+/main/docs/quick_start.md https://ninja-build.org/ https://ninja-build.org/
- nickelpro 3y ago> I'm a seasoned developer for the past 15-ish years and I haven't heard of ninja or gn before Then you also stopped learning about developments in your field 15 years ago. > I tried typing them in my command line (macos 12.4) and I would need to install them to use them. Ok? And? We don't pre-installed dev tools on consumer operating systems. You won't find valgrind or vcpkg either. > I was taught make in CS101 in college The tools we taught to beginners over a decade ago are perhaps not suitable in all cases, in fact they are suitable in very few cases. Not much Pascal usage either anymore. > the majority of other people's projects I've looked at use it. Then you live in a very tiny bubble.
- 3y ago
- markhahn 3y agonice: friendly but also succinct. how many people are using systems like cmake (or worse) who would be fine with just make (used right)?
- 01100011 3y agoNice to see some love for make. Sure, make syntax sucks. It's arcane and hard to google much of what is going on. (Shower thought... a tool that lets you view a Makefile, giving explanatory tooltips for syntax elements would be great). But make is widespread, ancient and eternal. It isn't going anywhere, despite decades of new tools trying to take its place. It does many jobs relatively well. I'd go so far as to say that the platonic ideal of development is: make # Build the project make install # Install the project make clean # Clean the project build dir(s) and whatever new tool you use to actually build things should live behind make. You should have a really good reason to deviate from this. It's approachable and familiar to most devs(well, ok, older C/C++ devs). Sure, write your own custom tool that builds everything in a chroot jail or docker container, has sub tools for installing, running tests, etc... but please, put it behind a make frontend.
- ParetoOptimal 3y ago> the platonic ideal of development is: > make # Build the project > make install # Install the project > make clean # Clean the project build dir(s) IMO, that last step of cleaning is very not platonic ideal. However... maybe a reality of incremental development? The first step for me in a lot of cases is just trying a project to see if it's worth my time, typically that would just have been: cd /tmp git clone http://foo/some/project.git http://foo/some/project.git cd project # build steps, perhaps in a good case your make example above, a dockerfile, or nix flake These days though I'm ecstatic when I see a Nix flake because I can reduce that to: nix run github:some/project Then for development I could even avoid cloning things myself (and sometimes do) with: nix develop github:some/project cd /tmp unpackPhase Make some change: genericBuild I guesss all this to say my platonic ideal includes getting more busy work out of the way and at least in the case of building, not having to worry about cache affecting reproducibility. I guess all of the above is to try and express my different idea of platonic ideal here and how Nix gives it to me (and could perhaps to you, for a cost).
- layer8 3y agoIf nothing else, make clean is useful to recover disk space not needed anymore, which can be substantial. It’s like deleting a cache or a tmp directory. It can also help to fix the build process by resetting it if something got stuck in the wrong state, but that’s not necessarily the primary purpose.
- andreynering 3y agoIf you're looking to an alternative, you could take a look at Task: https://taskfile.dev/ https://taskfile.dev/ https://github.com/go-task/task https://github.com/go-task/task
- Arcuru 3y ago+1 to this recommendation. Everyone likes to point to ‘just’ as a makefile replacement, but having tried both I slightly prefer Task.
- ataylor284_ 3y agoMake's killer feature was conditionally rebuilding based on changed dependencies. Back in the day, it was easy for a medium to largish software project to take many hours, or even days to fully rebuild. C and C++ were especially bad, especially "every file transitively includes every header" type projects. Make saved a lot of that pain, even if linking still sucked. I love make and still spin up a minimalist Makefile like in the article regularly for tiny projects, but I'd hate to have to actually maintain one for a real world project.
- ataylor284_ 3y agoYou could do something like this to get dependencies almost for free: .depends: $(SRCS) $(CC) $(CFLAGS) -MM $(SRCS) -o .depends -include .depends
- t43562 3y agoI see people now with long javascript builds that waste time doing many things repeatedly - it's quite ironic. Eventually people find crap to fill up the performance that's available. The problem is that now the tools aren't designed as composable bits that you can really parallelise with a makefile. C/C++ are designed that way and if they didn't use header files I'd consider them perfect from the build system's point of view :-). Golang and java for example are a nuisance.
- Too 3y agoA lot can be said about js tooling but honestly it works good once it is set up. Most modern js tooling provides --watch flags and hot module reloading. Vite is one popular option. Just save one file and the change instantly takes effect in the browser, even on a large project. Never seen any makefile based c++ project come close to that experience.
- t43562 3y agoI'm glad you had success. It wouldn't be difficult to do that with inotify to run make when a file changed. OTOH with webpack and transpiling and various other things I have watched people in my company take quite a long time to see a single change. I think they haven't sorted out the build right and I'm not enough of a javascript expert to do it for them. They prioritise various features over productivity and I think that's a mistake.
- hgs3 3y agoYou can use Autotools to generate your Makefiles. Autotools gets a bad rep, but I've found it perfectly fine for simple projects. To get started you only need two files, configure.ac and Makefile.am, you can ignore all the autogenerated stuff.
- bb88 3y agoThis page just makes me miss manpages in general. Not every manpage was great, but many were, and many could also be formatted, printed out, and placed into a binder for easier reading. What I enjoyed most about manpages is that the people that cared to write good documentation on things like bash. If you have groff installed you can: man -Tpdf man >man.pdf The only problem with groff is that it doesn't support system standard fonts.
- Tyr42 3y agoI honestly wish that make's default rules were available to import and view the source of, but we're disabled by default. Then a beginner could see what happens in a straightforward way, and then import the C rules. And we could all stop importing the VCS rules which aren't useful anymore.
- spc476 3y agoI don't know about POSIX make, but GNU make you can run "make -p -f/dev/null" to get a dump of all the built in rules it has. And if you don't want to run those, you can always use "make -r" to have it ignore built in rules.
- regurfvjtdf 3y agoThere’s so much of it in this thread I couldn’t possibly reply to all of it, but wow. Lot of make/shell wankery going on. No one is impressed with your pipe.
- heads 3y agoMake is the grilling of the hacker world. You just want to light up some quality lumpwood charcoal and get a nice smokey crust on some steaks, cook up some hamburgers, and crisp up some chicken for your guests over the course of an afternoon… …but oh no there is a constant stream of inquisitive folk coming over, beer in hand, suggesting cool adjustments / hacks / “improvements” to your setup. With grilling it’s actually pretty easy to defend oneself from this unsolicited advice. There’s only one grill after all. It’s not like it’s some company wide grill that anyone can make changes to solely by convincing one other person to sign off on their PR / MR / changeset. I’ve seen some very “clever” makefiles (and cmake lists.txt) over the years. It is hacker catnip for all manner of Jackson Pollock ad hoc scripting rubbish and I lament every commit that did anything except make the makefile less complex! All new software engineers should be given a shiny ball to play with when they get bored with their day job, lest they decide to take out their playful creativity on the build system instead.
- m463 3y agomake is very useful. The idea that things depend on each other and are built as a hierarchy is important. But it is the opposite of beauty, or elegance, or organization. Honestly all the millions of man months that have gone into writing makefiles should have gone into making make more usable. for example: all the implicit rules in this article? Nobody¹ uses them, they should just be explicit. You shold explicitly distinguish between a target and a file and a directory. don't let them depend on each other helter-skelter. indent, tabs, spaces. what a mess. requiring continuation lines and juggling return codes? macros that break with whitespace? I could go on, but ugh. I actually like cmake better than make. Just having IF statements and a flow makes things more understandable. But yeah, you trade one set of problems for another. look up "modern cmake" and you'll see the nitty-gritty between nice theory and ugly practice. [1] for practical purposes
- dmd 3y agoApparently the reasoning behind a bunch of Make’s warts is that by the time Stuart Feldman realized the problem, it already had half a dozen users, and, well, he wasn’t gonna go and break it for them!
- dextercd 3y agoI wish people actually defined the dependencies between .c and .h files and .h and other .h files. When a project doesn't do this, I always and up doing a `make clean && make` after getting stuck on a weird bug because I just assume it's not rebuilding something it should. Nowadays I use CMake and am really happy with it.
- kristjansson 3y agoOutside of simple C projects, a Makefile should be just a “here’s how to hold it” reference for the rest of your build system. Shoehorning in all of the build complexity is going to be overwrought, but a good “make dev; make clean; make build; make release” is a very welcome entry point for new or infrequent contributors
- psyclobe 3y ago30 year veteran here, funny enough I barely know anything about make. My first job we were building our own build utility because we had to build on windows netware, dos, Linux… soon after that it was all cmake, and I never had to use a make file for much of anything. Only recently have I come to be kinda fond of its insanely simple syntax but really make hasn’t been a requirement to native development since as long as I’ve been around getting paid.
- wruza 3y agoBack in 2000s I was all in the bash make unix camp. That was for ten years or so. Now I realize that it was just a religion, at least on my part. Python and Node and their libs are thousands of times better than all that nonsense (including arcanes like CMake). I have a whole folder of personal tools I’ve written in bash, and had a similar set for make. Some of them I don’t want to touch, but when I have to, I just rewrite them in Node with a little io/ipc helper library I created and it feels like a breath of fresh air.