sviamwvnb
sviamwvnb

Reputation: 139

`makefile` not working as expected

How can I have it so that when I run make that:

In other words, have make create the files it needs to build prog, if the dependencies have been modified

My current makefile:

CC = gcc
CFLAGS =
TARGET = prog

all: $(TARGET).s $(TARGET).c


$(TARGET).c: $(TARGET).s
    $(CC) $(CFLAGS) $(TARGET).s -o $(TARGET)

$(TARGET).s:
    $(CC) $(CFLAGS) -S -o $(TARGET).s $(TARGET).c


clean:
    $(RM) $(TARGET)
    $(RM) $(TARGET).s

It works if I have:

but does not work if I have manually modified prog.c
(make: Nothing to be done for 'all'.)

Upvotes: 1

Views: 147

Answers (1)

Picaud Vincent
Picaud Vincent

Reputation: 10982

You define your target: source in the reversed order. For instance, your assembly code depends on your C code, you must write:

$(TARGET).s: $(TARGET).c

however, you have written (wrong order)

$(TARGET).c: $(TARGET).s

Here is the version with the right order for "target: sources" :

CC = gcc
CFLAGS =
TARGET = prog

all: $(TARGET)


$(TARGET): $(TARGET).s
    $(CC) $(CFLAGS) $(TARGET).s -o $(TARGET)

$(TARGET).s: $(TARGET).c
    $(CC) $(CFLAGS) -S -o $(TARGET).s $(TARGET).c


clean:
    $(RM) $(TARGET)
    $(RM) $(TARGET).s

Upvotes: 3

Related Questions