JCRunner
JCRunner

Reputation: 145

Make did not work after I changed my file

These are my files: add.c add.h main.c makefile

This is makefile:

main:main.o add.o
    gcc -o main main.o add.o
main.o:$(@:%.o=%.c)
    gcc -o main.o -c main.c
add.o:$(@:%.o=%.c) $(@:%.o=%.h)
    gcc -o add.o -c add.c
.PHONY:clean 
clean:
    rm *.o -rf
    rm main -rf

Then after I change the main.c and make. But make told me this: make: `main' is up to date.

If I change my makefile:

main:main.o add.o
    gcc -o main main.o add.o
main.o:main.c
    gcc -o main.o -c main.c
add.o:$(@:%.o=%.c) $(@:%.o=%.h)
    gcc -o add.o -c add.c
.PHONY:clean 
clean:
    rm *.o -rf
    rm main -rf

Then after I change the main.c and make. It can work.

I donot know the reason.

Upvotes: 0

Views: 74

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136485

The dependencies in

main.o:$(@:%.o=%.c)
add.o:$(@:%.o=%.c) $(@:%.o=%.h)

are not valid make syntax.

Replace these two rules with one pattern (generic) rule:

%.o : %.c
    gcc -c -o $@ ${CPPFLAGS} ${CFLAGS} $<

The above rule is actually very similar to the built-in rule for compiling .o files from .c:

Compiling C programs

n.o is made automatically from n.c with a recipe of the form $(CC) $(CPPFLAGS) $(CFLAGS) -c.

In other words, you can remove your original rules for main.o and add.o and it should build correctly.

Upvotes: 1

Related Questions