I am writing a Makefile for compiling all *.c files in a directory into *.o . There are many *.c files so I don't want to do it on individual basis,
I tried
%.o: %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
but this isn't working ... please help me understand what's going wrong here ...
Your rule maps a single .c
file to a single .o
, and mirrors an existing implicit rule.
In order to generate all the .o
files corresponding to a set of .c
files, you can create a list of object file names from the list of .c
files, and then make a target that depends on that list:
SRC = $(wildcard *.c) # list of source files
OBJS = $(patsubst %.c, %.o, $(SRC)) # list of object files
objs : $(OBJS) # target
Then generate the object files with
make objs
This will build a .o
file for each .c
one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With