MakeFile使用隐式规则错误

时间:2014-06-30 12:43:36

标签: c++ makefile

尝试为我的项目组装Makefile。我使用Implicit Rules作为目标文件,但运行make compile失败并生成以下错误:

g++ -Wall -pedantic -std=c++11 -Wno-long-long -O0 -ggdb ./main.o ./AI.o ./BoardData.o ./Circle.o ./Coordination.o ./Cross.o ./GameField.o ./GameLogic.o ./GraficObject.o ./INetwork.o ./IUserInterface.o ./LocalHuman.o ./LocalPlayer.o ./NetworkException.o ./NetworkTCP.o ./NetworkTCPClient.o ./NetworkTCPServer.o ./Player.o ./PlayerRotator.o ./RemotePlayer.o ./Stone.o ./UserInterfaceNcurse.o -o ./game -lcurses
g++: error: ./main.o: No such file or directory
g++: error: ./AI.o: No such file or directory
g++: error: ./BoardData.o: No such file or directory
g++: error: ./Circle.o: No such file or directory
g++: error: ./Coordination.o: No such file or directory
g++: error: ./Cross.o: No such file or directory
g++: error: ./GameField.o: No such file or directory
g++: error: ./GameLogic.o: No such file or directory
g++: error: ./GraficObject.o: No such file or directory
g++: error: ./INetwork.o: No such file or directory
g++: error: ./IUserInterface.o: No such file or directory
g++: error: ./LocalHuman.o: No such file or directory
g++: error: ./LocalPlayer.o: No such file or directory
g++: error: ./NetworkException.o: No such file or directory
g++: error: ./NetworkTCP.o: No such file or directory
g++: error: ./NetworkTCPClient.o: No such file or directory
g++: error: ./NetworkTCPServer.o: No such file or directory
g++: error: ./Player.o: No such file or directory
g++: error: ./PlayerRotator.o: No such file or directory
g++: error: ./RemotePlayer.o: No such file or directory
g++: error: ./Stone.o: No such file or directory
g++: error: ./UserInterfaceNcurse.o: No such file or directory
make: *** [game] Error 1

这是我的MakeFile:

CXX=g++
CXXFLAGS=-Wall -pedantic -std=c++11 -Wno-long-long -O0 -ggdb
CXXLIBS=-lcurses
OBJECTS= ./main.o ./AI.o ./BoardData.o ./Circle.o ./Coordination.o ./Cross.o \
    ./GameField.o ./GameLogic.o ./GraficObject.o ./INetwork.o ./IUserInterface.o \
    ./LocalHuman.o ./LocalPlayer.o ./NetworkException.o ./NetworkTCP.o \
    ./NetworkTCPClient.o ./NetworkTCPServer.o ./Player.o ./PlayerRotator.o \
    ./RemotePlayer.o ./Stone.o ./UserInterfaceNcurse.o
DOXYFILE=./doxyfile
BINARY=./game
SOURCE_DIR=./src/
DOC_DIR=./doc/

################################################################################

.PHONY : all
.PHONY : compile
.PHONY : run
.PHONY : clean
.PHONY : doc

################################################################################

all: $(OBJECTS) $(DOXYFILE)
    make compile
    make doc

compile: $(BINARY)

run: $(BINARY)
    $(BINARY)

clean:
    -rm -rf $(DOC_DIR) $(BINARY) $(OBJECTS)

doc: $(DOXYFILE)
    -mkdir $(DOC_DIR)
    doxygen $(DOXYFILE)

################################################################################

$(BINARY): $(OBJECTS)
    $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(BINARY) $(CXXLIBS)

################################################################################

## Using Implicit Rules for all object files
## Generated by g++ -MM ./src/*.cpp

main.o: ./src/main.cpp ./src/IUserInterface.h ./src/Coordination.h \
 ./src/UserInterfaceNcurse.h ./src/GraficObject.h ./src/Cross.h ./src/Stone.h \
 ./src/Circle.h ./src/GameField.h ./src/LocalHuman.h ./src/LocalPlayer.h \
 ./src/Player.h ./src/INetwork.h ./src/GameLogic.h ./src/BoardData.h ./src/AI.h \
 ./src/RemotePlayer.h ./src/PlayerRotator.h ./src/NetworkTCPClient.h \
 ./src/NetworkTCP.h ./src/NetworkException.h ./src/NetworkTCPServer.h
AI.o: ./src/AI.cpp ./src/AI.h ./src/LocalPlayer.h ./src/Player.h \
 ./src/IUserInterface.h ./src/Coordination.h ./src/INetwork.h ./src/GameLogic.h \
 ./src/BoardData.h ./src/Stone.h ./src/GraficObject.h
BoardData.o: ./src/BoardData.cpp ./src/BoardData.h ./src/Coordination.h
Circle.o: ./src/Circle.cpp ./src/Circle.h ./src/Stone.h ./src/GraficObject.h \
 ./src/IUserInterface.h ./src/Coordination.h
Coordination.o: ./src/Coordination.cpp ./src/Coordination.h
Cross.o: ./src/Cross.cpp ./src/Cross.h ./src/Stone.h ./src/GraficObject.h \
 ./src/IUserInterface.h ./src/Coordination.h
GameField.o: ./src/GameField.cpp ./src/GameField.h ./src/GraficObject.h \
 ./src/IUserInterface.h ./src/Coordination.h
GameLogic.o: ./src/GameLogic.cpp ./src/GameLogic.h ./src/BoardData.h \
 ./src/Coordination.h
GraficObject.o: ./src/GraficObject.cpp ./src/GraficObject.h \
 ./src/IUserInterface.h ./src/Coordination.h
INetwork.o: ./src/INetwork.cpp ./src/INetwork.h ./src/Coordination.h
IUserInterface.o: ./src/IUserInterface.cpp ./src/IUserInterface.h \
 ./src/Coordination.h
LocalHuman.o: ./src/LocalHuman.cpp ./src/LocalHuman.h ./src/LocalPlayer.h \
 ./src/Player.h ./src/IUserInterface.h ./src/Coordination.h ./src/INetwork.h \
 ./src/GameLogic.h ./src/BoardData.h ./src/Stone.h ./src/GraficObject.h
LocalPlayer.o: ./src/LocalPlayer.cpp ./src/LocalPlayer.h ./src/Player.h \
 ./src/IUserInterface.h ./src/Coordination.h ./src/INetwork.h ./src/GameLogic.h \
 ./src/BoardData.h ./src/Stone.h ./src/GraficObject.h
NetworkException.o: ./src/NetworkException.cpp ./src/NetworkException.h
NetworkTCPClient.o: ./src/NetworkTCPClient.cpp ./src/NetworkTCPClient.h \
 ./src/NetworkTCP.h ./src/INetwork.h ./src/Coordination.h \
 ./src/NetworkException.h
NetworkTCP.o: ./src/NetworkTCP.cpp ./src/NetworkTCP.h ./src/INetwork.h \
 ./src/Coordination.h
NetworkTCPServer.o: ./src/NetworkTCPServer.cpp ./src/NetworkTCPServer.h \
 ./src/NetworkTCP.h ./src/INetwork.h ./src/Coordination.h \
 ./src/NetworkException.h
Player.o: ./src/Player.cpp ./src/Player.h ./src/IUserInterface.h \
 ./src/Coordination.h ./src/INetwork.h ./src/GameLogic.h ./src/BoardData.h \
 ./src/Stone.h ./src/GraficObject.h
PlayerRotator.o: ./src/PlayerRotator.cpp ./src/PlayerRotator.h ./src/Player.h \
 ./src/IUserInterface.h ./src/Coordination.h ./src/INetwork.h ./src/GameLogic.h \
 ./src/BoardData.h ./src/Stone.h ./src/GraficObject.h
RemotePlayer.o: ./src/RemotePlayer.cpp ./src/RemotePlayer.h ./src/Player.h \
 ./src/IUserInterface.h ./src/Coordination.h ./src/INetwork.h ./src/GameLogic.h \
 ./src/BoardData.h ./src/Stone.h ./src/GraficObject.h
Stone.o: ./src/Stone.cpp ./src/Stone.h ./src/GraficObject.h \
 ./src/IUserInterface.h ./src/Coordination.h
UserInterfaceNcurse.o: ./src/UserInterfaceNcurse.cpp \
 ./src/UserInterfaceNcurse.h ./src/IUserInterface.h ./src/Coordination.h \
 ./src/GraficObject.h

1 个答案:

答案 0 :(得分:1)

您需要更多地依赖自动化工具Make为您提供。

我认为您真正的问题是您的源文件位于src目录中,而您的OBJECTS变量会将它们列在当前(.)目录中。

此外,您实际上是递归调用自己的makefile,这是可以避免的。

使用合适的CXX*变量也是+1 ......

好现在:

BINARY  :=  game # `:=` operator prevents further expansion

DOC_DIR :=  doc
DOC     :=  doxyfile

SRC_DIR :=  src
SRC     :=  $(wildcard $(SRC_DIR)/*.cpp) # Automatically lists all .cpp files

OBJ_DIR :=  build
OBJ     :=  $(SRC:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o) # i.e. src/toto.cpp -> build/toto.o

DEP     :=  $(OBJ:.o=.d) # i.e. build/toto.o -> build/toto.d

# No need to redefine $(CXX) which already contains "g++"

CPPFLAGS    :=  -MMD -MP # Will auto-generate dependencies along with the linking step

CXXFLAGS    :=  -Wall -pedantic -std=c++11 -Wno-long-long -O0 -ggdb

LDFLAGS     :=  # -L flags

LDLIBS      :=  -lcurses # -l flags

.PHONY: all doc run clean

# We want to build the binary and the doc when typing "make"
all: $(BINARY) doc

# The binary requires compiling the sources
$(BINARY): $(OBJ)
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

# We want to create the "build" directory only once, before compiling the source files
# Create a build/.o and build/.d files for a given src/.cpp file, with dependency generation
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<

$(OBJ_DIR):
    @mkdir $@

-include $(DEP) # includes auto-generated dependency rules

doc:
    @mkdir -p $(DOC_DIR) # -p to avoid getting an error if it exists
    doxygen $(DOC)

run: $(BINARY)
    ./$(BINARY)