Vijay Katoch
Vijay Katoch

Reputation: 558

patsubst and compiling source files

As I understand the below lines in my makefile should compile all source files in current directory

SRC=Connection.cpp Request.cpp SessionHandler.cpp
OBJS=$(patsubst %.cpp,obj/%.o,$(SRC))

$(OBJS) : | obj
obj:
        @mkdir -p $@
obj/%.o : %.cpp
        g++ -std=c++11 -c $< -o $@

But only first file in $(SRC) gets compiled and place object in ./obj

g++ -std=c++11 -c Connection.cpp -o obj/Connection.o

What I am missing here ? Thanks.

Upvotes: 1

Views: 527

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80961

There can only be one default goal in a makefile.

Controlling the default goal is discussed in the GNU Make Manual in the Arguments to Specify the Goals section.

The crucial bit of which, for this question, is:

By default, the goal is the first target in the makefile (not counting targets that start with a period).

In your case the default target is therefore obj/Connection.o because this bit of your makefile

$(OBJS) : | obj

expands to

obj/Connection.o obj/Request.o obj/SessionHandler.o: | obj

which is equivalent to

obj/Connection.o: | obj
obj/Request.o: | obj
obj/SessionHandler.o: | obj

To get all your object files built by default you want to replace

$(OBJS) : | obj

with

.PHONY: all
all: $(OBJS)

Upvotes: 1

Related Questions