20 ms·
Today's simple makefiles are the end result of lessons hard learned. You'd be horrified to see what the output of imake looked like. From memory here's a Make
by syncsynchalt 9y ago
Today's simple makefiles are the end result of lessons hard learned. You'd be horrified to see what the output of imake looked like.
From memory here's a Makefile that serves most of my needs (use tabs):
SOURCE=$(wildcard *.c)
OBJS=$(patsubst %.c,%.o, $(SOURCE))
CFLAGS=-Wall
# define CFLAGS and LDFLAGS as necessary
all: name_of_bin
name_of_bin: $(OBJS)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -o $@ $^
clean:
rm -f *.o name_of_bin
.PHONY: clean all
- wyldfire 9y agobetter still, IMO -- capitalize on those sweet, sweet implicit rules. CFLAGS=-Wall # define CFLAGS, CXXFLAGS, LDLIBS and LDFLAGS as necessary all: name_of_bin name_of_bin: a.o b.o c.o clean: $(RM) -f *.o name_of_bin .PHONY: clean all
- MereInterest 9y agoI don't really like those, because then it makes it harder to separate out the source and the build directory. VPATH is a partial solution, but I find it much easier to have a single explicit pattern rule, rather than using both implicit pattern rules and modification of those implicit pattern rules.
- syncsynchalt 9y agoThat's fair. In actual projects of more than a few source files I do copy/paste some boilerplate to give me a build dir, dependency files, and so on. I just thought this (actual real-world example that I use for smallish projects) would help someone.
- syncsynchalt 9y agoThanks... I suspected I was just redefining a built-in pattern rule, but I put it there anyway just so I can remember whether it's LDFLAGS (linker) vs LFLAGS (lexer). Plus I was thinking maybe less magic for someone trying to make sense of it. Is there no command needed for name_of_bin to be linked? Does it automatically figure it out based on *.o dependencies?
- spc476 9y agoYes it does. In fact, with GNU make, if you have a single file, hello.c, you can type "make hello" without a makefile and it will just work.
- jwilk 9y ago$(RM) is defined as "rm -f", so the extra -f is not needed. Personally, I would just write "rm -f" explicitly. I don't see any advantages of using $(RM).
- fiatjaf 9y agoThis is totally incomprehensible to me.
- ycombobreaker 9y agoSounds like a learning opportunity to me. It's worthwhile in the long term, as make will outlive most of us.
- dmitriid 9y agoThere are next to none good learning resources on Makefiles. Most are extremely tersely written, have next to no description of how various parts tie together and assume everyone builds C/C++.
- ycombobreaker 9y agoThe GNU make manual is a good place to start. It's not a tutorial, but it is fairly short. If you're on a different platform it's not exact, but most of the important material applies to POSIX make implementations. Replace your own toolchain for the CC and related commands... really anything that takes multiple files as input and emits one file as output should fit the paradigm.
- dmitriid 9y ago> If you're on a different platform it's not exact, but most of the important material applies to POSIX make implementations. Which parts are "most of the material" though? ;) > Replace your own toolchain for the CC and related commands... really anything that takes multiple files as input and emits one file as output should fit the paradigm. I tried to. It's messy and undebuggable if you run into problems. Especially with tools that already look at the whole project (such as Typescript). We ended up using Makefiles as just "command launchers" with targets that basically look like target: invoke-tool
- JdeBP 9y agoThat is only true if one does not believe in reading, or makes no effort at all to look for such resources. There are entire books on this subject. * Clovis L. Tondo, Andrew Nathanson, and Eden Yount (1994). Mastering MAKE: a guide to building programs on DOS, OS/2, and UNIX systems. Prentice Hall. * Robert Mecklenburg (2004). Managing Projects with GNU Make. Nutshell Handbooks. O'Reilly Media. ISBN 9780596552541. * John Graham-Cumming (2015). The GNU Make Book. No Starch Press. ISBN 9781593276492.
- syncsynchalt 9y agoI just noticed I'm missing a "-c" flag in the .c -> .o compiler command. Sorry, just banged it out from memory on my phone, I didn't actually test it!
- Too 9y agoBroken. Update header file => no rebuild....