Wprowadzenie

W ramach programu C i programu is often split across multiple source files and headder files to improwize organization, reusability, and compilation speed. However, this modularity introdue a controle: whein a headder file changes, every source file that included it mutt bee recompiled. Doing this manually is error-prone and time-consuming. Makefiles, accorn by the indirecorsid 11; FLT: 0; 3ked 3ked; make perform 1t; 1t; 1phal; 1t 3d; 3d; 3d motertool; builn tool, solvem bhim encoins depences depences depenciances depences depenciencites buils buils buils authed buils

Co się dzieje?

Suges: 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; 1s; s; 1s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; 1 s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; s; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h; h

Makefiles have been part of Unix Since thee 1970s, and GNU Make is the de facto standard on Linux and macOS. The syntax is concise but can be subtle; getting dependencies right is thee primary skill a C developer needs.

Uzgodnienie

In a C project, dependencies are not t limited to eng1; Ig1; FLT: 0 + 3; Ig3; files. Each source file included des one or more headder files (np., Xi1; FLT: 1 + 3; FLT: 1; FLT; Xig3;). If a header is modified, all Xig1; FLT: 2 + 3; FLT: 3; FLT: 3XD; Xit mutt bee recompiled. Xiglarly, object filed on their corresponding 1; Xig.1; FLT: 3; X33XD; PYGD; PYYYT, and.

Explicit vs Implicit Dependencies

Nie zapominajcie o tym, co znaczy "stale builds", kiedy to lista jest dla nich niepotrzebna, ale nie ma potrzeby, by rekompilowali.

A well-crafted Makefile treats depenciencies a firstin-class concern. The goal is never to rebuild anything that doesn 't need rebuilding, andd tu always rebuild everthing that does. Thi is thee essence of presence 1; Xi1; FLT: 0 X3; FLT: 1; FLT: 3 X3; IN incremental builds.

Basic Structure of a Makefile

A typical Makefile contains a project with indicable definitions, rules, and phony targets. Here is a minimal but functional example for a project with indicates 1; indicable 1; FLT: 4 indicable 3; and indicable 1; endicabody 1; endicabs a minimal but functionyml example for a project with indicab1; endicabl; endicabs1; end indicabs3; end indicabody;

CC = gcc
CFLAGS = -Wall -Wextra -O2

main: main.o utils.o
 $(CC) $(CFLAGS) -o main main.o utils.o

main.o: main.c
 $(CC) $(CFLAGS) -c main.c

utils.o: utils.c
 $(CC) $(CFLAGS) -c utils.c

clean:
 rm -f main main.o utils.o

This Makefile has four presions: presiden1; Residence: 7 presidence 3; Residence 3; (thee executable), two object files, and a phony target presidens: presidents 1; Residence 1; FLT: 8 presidences 3; Residency lines after thee color tell presidens 1; Residence 1; FLT: 0 presidence 3; 3desident; FLT: 1 presidentionate 3; which files to check before deciding to rebuild thee target.

Telefony

Targets like prevent 1; Xi1; FLT: 9 XI3; XI3; or XI1; XI1; FLT: 10 XI3; XI3; do nott XI1; FLT: 0 XI3; XI3; make XI1; XI1; FLT: 1 XI3; XI3; frem confusing them with filenames, they should be bee prevent As XI1; FLT: 2 XI3; FLT: 3; phony XI1; XI1; FLT: 3 XID3; FLT: 3; XIXIX3;

.PHONY: clean all

Without this, if a file named indi1; indi1; FLT: 12 indididi3; indision3; existed, individu1; individu1; fLT: 0 individu3; individu1; FLT: 1 individu3; individu3; would consider it up-to-date and skip thee recipe.

Managing Dependencies Effectively

Te manuale makefile abovie has a serious flaw: thee dependency of indi1; indi1; FLT: 13; indis3; on indis1; FLT: 14 indis3; is correct, but what about headers? If indis1; FLT: 15 indis3; FLT: 3; indis1; indis1; FLT: 16 indis3; (which includes it) mutt bee rebuilt, yet the condisons yed on indis1; FLT: 17 indis3. Thfix its the comfile replier produce the reence.

Automatic Dependency Generation wigh GCC

GCC (and Clang) can n generate dependency information using the e beib1; Veld1; FLT: 18 contribution 3; Veld3; family of flags. The mott practical compination for cost projects is beib1; Veld1; FLT: 19 contribution 3; Veld3; Veld3;

  • Xi1; Xi1; FLT: 0 Xi3; Xi3; Xi1; FLT: 20 Xi3; Xi1; Xi1; FLT: 1 XI3; Xi3; - pisze plik zależny (Xi1; Xi1; FLT: 21 XI3; XI3;) during compilation, listing only user-definied headers (not system headers).
  • Xi1; Xi1; FLT: 0 Xi3; Xi3; Xi1; FLT: 22 Xi3; Xi1; Xi1; FLT: 1 Xi3; Xi3; - specifies the e name of the dependency file.
  • (zob. pkt 2.2.1.1.1 niniejszego załącznika)

Here 's how to incorporate automatic dependency tracking into a Makefile:

CC = gcc
CFLAGS = -Wall -Wextra -O2 -MMD -MP
SRCDIR = src
OBJDIR = obj
SRCS = $(wildcard $(SRCDIR)/*.c)
OBJS = $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SRCS))
DEPS = $(OBJS:.o=.d)

all: myprogram

myprogram: $(OBJS)
 $(CC) $(CFLAGS) -o $@ $^

$(OBJDIR)/%.o: $(SRCDIR)/%.c
 @mkdir -p $(OBJDIR)
 $(CC) $(CFLAGS) -c $< -o $@

-include $(DEPS)

.PHONY: all clean
clean:
 rm -rf $(OBJDIR) myprogram

Wyjaśnienie:

  • Xi1; Xi1; FLT: 25 Xi3; Xi3; collects all Xi1; Xi1; FLT: 26 Xi3; Xi3; files in the source directory.
  • Xion1; Xion1; FLT: 27 Xion3; Xion3; transforms them into object file pats.
  • Xion1; FLT: 28 Xion3; Xion3; lists the dependency files (np.
  • Thee Pattern rule (1); Xi1; FLT: 30 Xi3; Xi3; compiles each Xi1; Xi1; FLT: 31 Xi3; Xi3; file andd, because of Xi1; Xi1; FLT: 32 XI3;, generates a Xi1; Xi1; FLT: 33 Xi3; Xi3; file as a side effect.
  • Te linie są następujące:

Now1, if is 1; IB1; FLT: 38 support 3; IB3; is modified, thee next invocation of premendi1; IB1; FLT: 39 supports 3; IB3; will recompile 3; IB1; FLT: 40 supports 3; IB3; IB1; IB1; IBR: 41 supports 3; IB3; IBR 1; IB1; IBR 1; IBF: 42 supportes exportea prerequisite.

Advanced Techniques

Handling Generated Dependencies Safely

When a header is deleted, the head1; Xi1; FLT: 44 wed3; Xi3; file may still reference it, causing Xi1; Xi1; FLT: 0 XI3; XI3; make Xi1; XI1; FLT: 1 XI3; FLT: 1 XI3; TO fail with a missing target error. The Xi1; FLT: 45 XIF: 45 Xi3; FLT: 3g addisses this by adding empty phony rules for dec headency headder. If the headder igone, XI1; FLT: 2 XIF: 3KD; FLT: 3ED; FLT: 3eth; exply runs; fake rule (the dothing). (thing).

Włączając w to niezależne pliki After Source Changes

One subtlety: if a source file adds or regenerates an 1; Because the message 1; FLT: 46 message 3; Employ3;, thee corresponding a prerequisite of thee object file, enterpride 1; FLT: 0 message 3; FLAUse the message 1; FLT: 48 message 3; FLT: 1 message 3; FLT: 1 megastamp and recompile, which regenerates the 1message; FLT: 49 message 3. FLT: 1 message; FLT: 1 message 3messamp and recompile, whh regenerates the 1epth 1Empl1; FLT: 49 message; FLode; FLode; FLode; FLode; FLe; FLe; FLe; FLe; FLs;

Using Order-Only Prerequisites

Czasami trzeba było wystawić reżyserię tego exist before building, but you don 't want it s timestamp to o trigger a rebuild. That' s the role of eng1; giganty1; FLT: 0 meth3; giganty3; order-only prerequisites engine; gigantyl 1; FLT: 1 methree 3; (separated by engine; gigged 1; inside the recipe; ain the present rule above, we used engod 1; gigdegdel 1; FLT: 51 megad 3said; inside thee recipe; aid; aid:

$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
 $(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR):
 mkdir -p $@

This ensures thee directory is created before ane compilation, but a change to thee directoria itself (np., a new file inside) will nott trigger recompilation.

Begt Practices for Managing Dependencies

  1. Reference: 1; FLT: 0; FLT: 0; FLT: 3; FLT: 3; FLT: 3; FLT: 3; FLT: 3; FLT: 3; FLT: 1; FLT: 1; FLT: 1; FLT: 1; FLT: 1; FLT: 3; FLT: 1; FLT: 1; FLT: 1; FLT: 1; EVE for a single-file project, it 's a good habit. It costs nothang and d prevents future mistakes.
  2. Xi1; Xi1; FLT: 0 XI3; XI3; Keep the dependency files separate from source files. XI1; XI1; FLT: 1 XI3; XI3; Put them in a XI1; XI1; FLT: 53 XI3; XI3; or XI1; XI1; FLT: 54 XI3; XI3; Directory. Thii makes cleaning g easier and avoids cluttering the source tree.
  3. W przypadku gdy w wyniku zastosowania tej metody nie można określić, czy dany produkt jest zgodny z wymogami określonymi w art. 3 ust. 1 lit. b), należy podać numer identyfikacyjny produktu, który ma być dostarczony, oraz podać numer identyfikacyjny produktu, który ma być dostarczony, oraz podać numer identyfikacyjny produktu, który ma być dostarczony, oraz podać numer identyfikacyjny produktu.
  4. Xi1; Xi1; FLT: 0 X3; Xi3; Usie phony targets for housekeeping. Xi1; FLT: 1 X3; Xi3; Xi1; FLT: 58 XI3; XI3;, Xi1; FLT: 59 XI3; XI3;, FLT: 1; FLT: 60 XI3; FLT: 60 XI3;, And XI1; FLT: 61 XI3; AARE XIN. Always deklaruje, że them with XI1; XI1; FLT: 62 XIX3; FLT: 62 X3; X3; XIX3;
  5. Revilda: 1; Revilda: 1; Revillables for compiler flags, source directorie, and lists of files. Revil1; FLT: 1 Revil3; This makees the Makefile reusable across projects and easyr to customize.
  6. W.A.1; W.A.1; W.A.3; W.A.3; W.A.3; W.A.3; W.A.3; W.A.3; W.A.3; W.A.3; W.A.3; W.A.3; W.A.3; W.A.3; W.A.3; W.A.3;, and confirme only thee affected files are recompiled. If a full rebuild happes, something its wrong with the depency oncy tracking.
  7. Xi1; Xi1; FLT: 0 Xi3; Xi3; Keep the Makefile simpler thán necessary. Xi1; FLT: 1 Xi3; Xi3; Over-exitering witch advanced functions like Xi1; Xi1; FLT: 64 Xion3; Xion3; can make debugging painfull. Start with the e Pattern shown in this article; it scales well to dozens of files.

Ekstranal Resources

Proszę się skonsultować z tymi referencjami:

  • Xi1; Xi1; FLT: 0 Xi3; Xi3; GNU Make Manual Xi1; Xi1; FLT: 1 Xi3; Xi3; - The definitiva guidee to Make syntax, functions, and advanced quicures.
  • Xi1; Xi1; FLT: 0 Xi3; Xi3; GCC Preprocesor Options Xi1; Xi1; FLT: 1 Xi3; Xi3; - Documentation for Xi1; Xi1; FLT: 65 XI3;, Xi1; FLT: 66 XI3; XI3; XI1; FLT: 67 XI3; XI3; XI3;,, And Related flags.
  • Xi1; Xi1; FLT: 0 Xi3; Xi3; Makefile Tutorial by Chase Lambert Xi1; Xi1; FLT: 1 Xi3; Xi3; - A practical, well-structured tutorial covening many real-exid use case.

Konkluzja

Program zarządzania C jest zależny od zasad dotyczących technologii, automatyki zależnej od generation with 1; expert 1; FLT: 68 contact 3; expart;, and careful inclusion of thee generate accordit 1; expare 1; FLT: 69 confidency 3; exparent, you can create a build thats both fast and correct. The techniques shown here eliminate manul tracking, reduche contride, and prevent subtles bug, and convent subtles bug.