Makefile and week 7 review
In the previous lesson you compiled in two steps typing the commands by hand. With five or ten files that is a nightmare: you make mistakes, forget to recompile one or recompile everything unnecessarily. The make tool automates the process. It reads a file called Makefile where you describe which files depend on which and how each one is built, and it decides by itself what must be rebuilt by comparing modification dates. You type make and, if nothing changed, it tells you there is nothing to do.
Rules, variables and phony targets
A Makefile is made of rules with the form target: dependencies, followed by recipe lines that must start with a tab (not spaces; it is the most famous make error: "missing separator"). make rebuilds the target if it does not exist or if any dependency is newer. Variables (CC, CFLAGS) avoid repeating options. Targets that are not files, like clean or run, are declared with .PHONY so make does not confuse them with a file of the same name. By convention, the first target is the one built when you type plain make.
CC = gcc
CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -g
OBJS = main.o mathutil.o
app: $(OBJS)
$(CC) $(OBJS) -o app
main.o: main.c mathutil.h
$(CC) $(CFLAGS) -c main.c
mathutil.o: mathutil.c mathutil.h
$(CC) $(CFLAGS) -c mathutil.c
.PHONY: run clean
run: app
./app
clean:
rm -f $(OBJS) appWith this file, make compiles and links everything; if you then edit only mathutil.c, make recompiles only mathutil.o and links again. Notice that main.o also depends on mathutil.h: if you change the header, main.c must be recompiled even though the .c did not change. Forgetting to list a header as a dependency produces a very confusing error: you change a struct in the header and the program behaves strangely because some objects use the old version. In that case, make clean and make fix the symptom, but the real solution is to declare the dependencies properly.
A shorter version
When there are many files, writing a rule for each gets tiring. A pattern rule, %.o: %.c, serves all of them: $< is the first dependency and $@ is the target. With the options -MMD -MP, gcc also generates .d files with the header dependencies, and the line -include $(OBJS:.o=.d) reads them, so make knows the real dependencies without you writing them by hand. Adding a new module comes down to adding its .o to the OBJS variable.
CC = gcc
CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -g -MMD -MP
OBJS = main.o mathutil.o
app: $(OBJS)
$(CC) $(OBJS) -o app
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
-include $(OBJS:.o=.d)
.PHONY: run clean
run: app
./app
clean:
rm -f $(OBJS) $(OBJS:.o=.d) appWeek 7 review
- fopen/fclose with a NULL check; modes r, w and a.
- fgets + strcspn + sscanf to read text line by line safely.
- A module = .c (definitions) + .h (declarations) with an include guard.
- static makes a function private; extern declares a shared variable.
- gcc -c produces objects; the linker joins them.
- make rebuilds only what changed; the tab is mandatory in recipes.
理解度チェック
問題 1 / 7Which fopen mode erases the file's previous content?
✎ マイノート
このブラウザにのみ保存されます。ログインするとアカウントに保存できます。0/5000 文字