1

My makefile will not check if there has been any updates and just compiles if it has more than a single source file added in. It works fine with just a single source file.

It seems that it's any source file that's not listed as the first one will always be recompiled and linked.

SOURCES=myclass.cpp mylock.cpp
EXECUTABLE=locktest
LIBRARIES=-pthread
CFLAGS=-Wall
CXX=g++
DIR=host/
EXE=$(EXECUTABLE)

OBJECTS=$(SOURCES:%.cpp=$(DIR)%.o)

$(EXE): $(OBJECTS)
    $(CXX) -o $@ $(OBJECTS) $(LIBRARIES)

$(DIR)%.o: %.cpp $(DIR)
    $(CXX) $(CFLAGS) -c $< -o $@ 

$(DIR):
    @mkdir $(DIR)

clean: 
    @rm $(OBJECTS) $(EXE)
    @rmdir $(DIR)

Output shows problem:

stud@pc:~/Desktop/Locktest$ make
g++ -Wall -c myclass.cpp -o host/myclass.o 
g++ -Wall -c mylock.cpp -o host/mylock.o 
g++ -o locktest host/myclass.o host/mylock.o -pthread
stud@pc:~/Desktop/Locktest$ make
g++ -Wall -c myclass.cpp -o host/myclass.o 
g++ -o locktest host/myclass.o host/mylock.o -pthread
4

2 回答 2

1

正如@lijat 指出的那样,当您在 中构建对象时$(DIR),操作系统会更新目录的修改时间,因此在此规则中:

$(DIR)%.o: %.cpp $(DIR)
    ...

先决条件$(DIR)总是看起来比除了最后一个目标之外的任何目标都更新。

如果您的 Make 版本足够新,则可以通过$(DIR)订购先决条件来解决此问题:

$(DIR)%.o: %.cpp | $(DIR)
    ...
于 2013-09-28T23:06:24.680 回答
1

当编译器在那里写入 .o 文件时,文件系统是否更新 DIR 目录上的修改时间

$(DIR)%.o: %.cpp $(DIR)

确保如果有任何更新该目录的修改时间,所有 .o 文件都将被重新编译。

于 2013-09-28T22:15:55.060 回答