Makefile pattern rules differences

152 views Asked by At

What is the difference between .cpp.o:, .o: and %.o: %.c?
Here's a simple Makefile example:

CC=g++
CFLAGS=-c -Wall
SOURCES=file1.cpp file2.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=program

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(OBJECTS) -o $@

#.o:
#.cpp.o:
%.o: %.c
    $(CC) $(CFLAGS) $< -o $@

all: $(SOURCES) $(EXECUTABLE)

clean:
    rm -rf $(OBJECTS) $(EXECUTABLE)

I have noticed that the output is same, but I guess they are interpreted on a different way internally. Is there a preferred way of doing this?

1

There are 1 answers

2
dlask On BEST ANSWER
.cpp.o:    # build *.o from *.cpp (old style notation)

%.o: %.c   # build *.o from *.c (new style notation)

Both work but the new style is more powerful because it allows you to write more complicated constructions thanks to pattern matching:

%.uuu: %.vvv %.www