user2836797
user2836797

Reputation:

Why does this make file only call one target?

I am new to make and I am trying to make a super simple build script. This is what I have:

.PHONY: all main
all:
    mkdir -p build && cd build

main: main.o install
    g++ -o main main.o

main.o: ../src/main.cpp
    g++ -c ../src/main.cpp

.PHONY: install
install: 
    mkdir -p build
    mv main.o build 

.PHONY: clean
clean:
    rm -r build/

I would expect it to call all followed by main. In actuality, here's what happens:

$ make
mkdir -p build && cd build

Only all is called and main is not ran. Why? I have main as a prerequisite after all in the .PHONY line. And help?

Upvotes: 2

Views: 826

Answers (1)

John
John

Reputation: 3520

.PHONY is not a real target (it is a special make construct), and does not cause its prerequisites to be run. Instead, the first real target mentioned is all, and if you just type make, it will invoke the all as the default target. Because all is not dependent on anything, it is the only target that is run.

You can add a line at the very top:

default: all main

which will cause both all and main to run (don't forget to add default to .PHONY. Notice though that you are not guaranteed that all will run before main. If you want to guarantee this, you would also have to add

main: all

which would force all to be run before main

Upvotes: 2

Related Questions