Home > Software design >  makefile not changing input filename
makefile not changing input filename

Time:04-28

I am trying to make a C program and have the following makefile:

CC=clang
CFLAGS=-std=c99 -Wall -pedantic
LDFLAGS=
LDLIBS=
OUT=nes_slop
SRC_DIR=src/
OBJ_DIR=obj/
SRCS=$(wildcard $(SRC_DIR)*.c)
OBJS=$(addprefix $(OBJ_DIR),$(notdir $(SRCS:.c=.o)))
MAKE=make
CLEAR=TRUE

all: clean $(OUT)

clean:
    rm -i $(OUT) $(OBJS) -f

$(OUT): $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o $(OUT)

$(OBJS): $(SRCS)
    $(CC) -c $(CFLAGS) $(LDLIBS) $< -o $@

It was all well and good until I had more than 1 .c file:

clang -c -std=c99 -Wall -pedantic -lncurses  src/gamestate.c -o obj/gamestate.o
clang -c -std=c99 -Wall -pedantic -lncurses  src/gamestate.c -o obj/main.o

so, somehow the source file is not being updated, it's always gamestate.c... what's wrong with my makefile? any help is appreciated, thank you

CodePudding user response:

In short, your rule should look something like this:

$(OBJS): %.o: %.c
    $(CC) -c $(CFLAGS) $(LDLIBS) $< -o $@

You may also want to read this for how to generate automatic dependencies (so if you change a header file, your c files will automatically regenerate as needed) There's a TL;DR section at the top of that page, if you're not interested in the details.

  • Related