GNU make and the list of objects

I have about 10 source files (.c) with headers (.h). Only two source files (.c) are associated with executable files (ELF) and contain the main function. Most files are compiled into (.o) and (re) objects used (statically linked) with executable files.

I tried to define a general rule for creating all objects:

%.o : %.c
    $(CC) $(CCFLAGS) -c -o $@ $<

This works great for sources compiled into objects. I don’t have to worry about updating the makefile every time I add a new source file.

But I do not know what would be the best way to create rules for creating executable files?

exec1 : object1.o object2.o object3.o
    call the linker
exec2 : object1.o object2.o object4.o object5.o
    call the linker

This will work; but when I include the new header (foo.h), I will also need to remember the list of objects for this rule (add foo.o).

- make (, ?)?

?

: , . ?

!

+4
1

wildcard ,

sources := $(patsubst %.c,%.o,$(wildcard *.c))

,

headers := $(wildcard *.h)

,

%.o : %.c $(headers)

GNU (https://www.gnu.org/software/make/manual/make.html) , .

0

Source: https://habr.com/ru/post/1648478/


All Articles