zdavid0123
zdavid0123

Reputation: 13

Makefile only tracks the first file

There are two .cpp files in my makefile:

test1.o : test1.cpp
        g++ test1.cpp -o test1.o

test2.o : test2.cpp
        g++ test2.cpp -o test2.o

After I make changes to both test1.cpp and test2.cpp and type make in the command line, only test1.o is recompiled, and nothing is done to test2.o.

However, if I swap the two blocks in the makefile, test2.o will be recompiled automatically and test1.o will not.

It seems like my makefile only keeps track of the first file. I wonder which part goes wrong.

Upvotes: 1

Views: 354

Answers (2)

By default make will build the first thing in the makefile, if you don't tell it what to build.

You can either run make test1.o test2.o to make both specifically, or add a fake target so it will make all by default - this is what many makefiles use.

Using .PHONY tells Make that all isn't a real file, so it doesn't misbehave just in case you have a file called all for some reason.

all: test1.o test2.o
.PHONY: all

Upvotes: 3

John3136
John3136

Reputation: 29266

Try adding a default target. A really simple starting point: all: test1.o test2.o

Upvotes: 3

Related Questions