2017-01-22 120 views
0

我的makefile有两条线路:makefile中的规则顺序很重要?

Rule_1

File.o : File.cpp 
    g++ -c File.cpp -o File.o 

Rule_2

File : File.o 
    g++ File.o -o File 

我想在任何File.cpp变化导致两File.oFile的再生。

Makfile:这是通过

Rule_2 
Rule_1 

但下面不工作,仅再生File.o

Rule_1 
Rule_2 

为什么?我注意到,我的makefile中没有使用任何all:。通过all: File以上两种方法都可以工作。

回答

2

我假设你的make工具是GNU Make。

除非指定命令行上的目标,如:

make File 

make默认会试图使第一目标 在makefile,它在你的问题的情况是File.o。 见How make Processes a Makefile

当您添加:

all: File 

上方,all是第一目标,这取决于File, 而这又取决于File.o

我建议你更换与:

.PHONY: all 

all: File 

Phony targets

+0

或者,如果OP(或任何人:))使用FreeBSD的化妆可以使用'.MAIN'目标(见HTTPS:/ /www.freebsd.org/cgi/man.cgi?make(1))。 – uzsolt