Home > database >  Compiling lot of .c files to .o files using makefile
Compiling lot of .c files to .o files using makefile

Time:10-18

I want to compile all .c files to .o files except some of them. Any help? Currently my commands are looking like this:

%.o: %.c
    $(CC) -C -o $(CFLAGS) $@ $^

but I made a

EXCEPTIONS2 = testadmin.c testclient.c testserver.c server_comm.c client_comm.c
TO_COMPILE = $(filter-out $(EXCEPTIONS2), $(wildcard *.c))

But i dont know how to put them together, any idea?

CodePudding user response:

Just add a target that depends on the object files you want to be created:

.PHONY: all
all: $(TO_COMPILE:%.c=%.o)

and that's all.

CodePudding user response:

You need to add a target that depends on those files. You can, for example, get all files in a directory like

SOURCEDIR := Some/Directory/You/Like
SOURCES := $(shell find $(SOURCEDIR) -name '*.c')
EXCEPTIONS2 = testadmin.c testclient.c testserver.c server_comm.c client_comm.c
TO_COMPILE = $(filter-out $(EXCEPTIONS2), $(SOURCES))

and then do something like:

%.o: %.c
    $(CC) -C -o $(CFLAGS) $@ $^

all: $(TO_COMPILE:%.c=%.o)
  • Related