0

My simple little makefile is exhibiting behavior which I'm not able to understand. If I touch any source file except Dictionary.cpp then no targets are built, and if I touch Dictionary.cpp then it compiles but doesn't link. Source files are in src/ object (.o) and dependencies (.d) are in obj/ and binary goes into bin/

If I rm obj/* then everything builds OK but the timestamps don't seem to be being picked up. Can anyone tell me where I'm going wrong?

The .d files seem to be being created correctly, here's Dictionary.d:

obj/Dictionary.o: src/Dictionary.cpp src/pch.h src/Types.h src/Util.h \
 src/Refcount.h src/Dictionary.h
src/Dictionary.cpp:
src/pch.h:
src/Types.h:
src/Util.h:
src/Refcount.h:
src/Dictionary.h:

Which looks correct to me. Here's the makefile:

sources =   Dictionary.cpp  \
            Util.cpp        \
            Tile.cpp        \
            Board.cpp       \
            Vec2.cpp        \
            Letter.cpp      \
            Random.cpp      \
            Server.cpp      \
            main.cpp

objects = $(patsubst %.cpp,obj/%.o,$(sources))
depends = $(patsubst %.cpp,obj/%.d,$(sources))

CXX = g++
CPPFLAGS = -Isrc -std=c++0x
CXXFLAGS = -c

-include $(depends)

bin/dictionary: $(objects)
    @echo Link...
    $(CXX) $(CPPFLAGS) $(objects) -o bin/dictionary -lrt

obj/%.o: src/%.cpp
    @echo [$*]
    @$(CXX) $(CPPFLAGS) $(CXXFLAGS) src/$*.cpp -o obj/$*.o
    @$(CXX) $(CPPFLAGS) -MM src/$*.cpp -MF obj/$*.d
    @mv -f obj/$*.d obj/$*.d.tmp
    @sed -e 's|.*:|obj/$*.o:|' < obj/$*.d.tmp > obj/$*.d
    @sed -e 's/.*://' -e 's/\\$$//' < obj/$*.d.tmp | fmt -1 | sed -e 's/^ *//' -e '    s/$$/:/' >> obj/$*.d
    @rm -f obj/$*.d.tmp
4

1 回答 1

1

您必须将其移到include最后,或者将bin/dictionary规则放在它之前,或者在包含之前添加一条all: bin/dictionary规则,或者其他东西。

或者,让自己永远运行make bin/dictionary,这也可以。

请记住,默认情况下,make 会尝试构建 makefile 中的第一个目标。因为您include在任何其他目标之前都有该行,所以included 文件定义的第一个目标将被视为默认目标,而这恰好是obj/Dictionary.o.

于 2013-05-04T12:18:34.653 回答