Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile to convert all *.c to *.o

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 ...

like image 373
Shraddha Avatar asked Feb 17 '15 10:02

Shraddha


1 Answers

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.

like image 174
juanchopanza Avatar answered Nov 04 '22 11:11

juanchopanza