Makefile:src目录中的所有.cpp和.h文件

时间:2018-05-24 10:49:28

标签: c++ makefile

我正在努力创建一个可以在一个src目录中使用所有.cpp和.h文件的makefile。我发现了很多关于makefile和目录的回答问题,但它们都有一个非常复杂的目录结构,所以我没有真正得到答案。到目前为止,我有这个:

CXX=g++
LD=g++
CXXFLAGS=-Wall -pedantic -std=c++14 -Wno-long-long -O0 -ggdb
LDLIBS=-lncurses

SRCDIR=src
OBJECTS=answer.o handler.o main.o question.o quiz.o ui.o uiCreateQuiz.o uiImportExport.o uiPrinter.o uiSolveQuiz.o

all: quiz run

compile: quiz

quiz: $(OBJECTS)
    $(LD) $^ $(LDLIBS) -o $@

%.o: $(SRCDIR)/%.cpp $(SRCDIR)/%.h
    $(CXX) $(CXXFLAGS) $(LDFLAGS) -c -o $@ $<

run:
    valgrind ./quiz

clean:
    rm -f $(OBJECTS) quiz

answer.o: answer.cpp answer.h
handler.o: handler.cpp handler.h quiz.h question.h answer.h
main.o: main.cpp ui.h handler.h quiz.h question.h answer.h uiPrinter.h uiSolveQuiz.h uiCreateQuiz.h uiImportExport.h
question.o: question.cpp question.h answer.h
quiz.o: quiz.cpp quiz.h question.h answer.h
ui.o: ui.cpp ui.h handler.h quiz.h question.h answer.h uiPrinter.h uiSolveQuiz.h uiCreateQuiz.h uiImportExport.h
uiCreateQuiz.o: uiCreateQuiz.cpp uiCreateQuiz.h handler.h quiz.h question.h answer.h uiPrinter.h
uiImportExport.o: uiImportExport.cpp uiImportExport.h handler.h quiz.h question.h answer.h uiPrinter.h
uiPrinter.o: uiPrinter.cpp uiPrinter.h
uiSolveQuiz.o: uiSolveQuiz.cpp uiSolveQuiz.h handler.h quiz.h question.h answer.h uiPrinter.h

结果是:

make: *** No rule to make target 'answer.cpp', needed by 'answer.o'.  Stop.

我应该做些什么来改变它?

2 个答案:

答案 0 :(得分:1)

这是我多年来制作的makefile结构:

CXX=g++
CFLAGS=-c -Wall -Wextra -Weffc++
LDFLAGS=-lall_my_libs    
SOURCES= \
    all/my/cpp/files.cpp \
    ...

OBJECTS=$(SOURCES:src/%.cpp=bin/%.o)
DEPS=$(OBJECTS:bin/%.o=bin/%.d)

all: my_binary

-include $(DEPS)

my_binary: $(OBJECTS)
    $(CXX) -o $@ $(OBJECTS) $(LDFLAGS)

bin/%.o: src/%.cpp
    $(CXX) -MD $(CFLAGS) -o $@ $<

clean:
    rm -rf bin/*.o bin/*.d my_binary

您可以将源文件放在src目录中。并且二进制文件放在bin目录中。

关于包含文件:-MD编译器pption生成依赖文件(* .d),可以使用-include $(DEPS)选项包含这些文件。这样你就不必再担心它们了。

答案 1 :(得分:1)

这是你的问题:

%.o: $(SRCDIR)/%.cpp $(SRCDIR)/%.h

answer.o: answer.cpp answer.h

answer.o取决于$(SRCDIR)/answer.cpp$(SRCDIR)/answer.hanswer.cppanswer.h。让所有人都找不到。

获得所需内容的最简单方法(假设为GNU Make)是使用VPATH告诉它搜索$(SRCDIR)先决条件:

CXX = g++
CXXFLAGS += -std=c++14 -Wall -Wextra -pedantic -Wno-long-long
CXXFLAGS += -O0 -ggdb
LIBS += -lncurses

OBJECTS = answer.o handler.o main.o question.o quiz.o ui.o
OBJECTS += uiCreateQuiz.o uiImportExport.o uiPrinter.o uiSolveQuiz.o

VPATH = src

all: run
compile: quiz

quiz: $(OBJECTS)
quiz: LINK.o=$(LINK.cc)

run: quiz
    valgrind ./quiz

clean:
    $(RM) $(OBJECTS) quiz

answer.o: answer.cpp answer.h
handler.o: handler.cpp handler.h quiz.h question.h answer.h
main.o: main.cpp ui.h handler.h quiz.h question.h answer.h uiPrinter.h
main.o: uiSolveQuiz.h uiCreateQuiz.h uiImportExport.h
question.o: question.cpp question.h answer.h
quiz.o: quiz.cpp quiz.h question.h answer.h
ui.o: ui.cpp ui.h handler.h quiz.h question.h answer.h uiPrinter.h
ui.o: uiSolveQuiz.h uiCreateQuiz.h uiImportExport.h
uiCreateQuiz.o: uiCreateQuiz.cpp uiCreateQuiz.h handler.h quiz.h
uiCreateQuiz.o: question.h answer.h uiPrinter.h
uiImportExport.o: uiImportExport.cpp uiImportExport.h handler.h quiz.h
uiImportExport.o: question.h answer.h uiPrinter.h
uiPrinter.o: uiPrinter.cpp uiPrinter.h
uiSolveQuiz.o: uiSolveQuiz.cpp uiSolveQuiz.h handler.h quiz.h
uiSolveQuiz.o: question.h answer.h uiPrinter.h

(我做了一些小的改进 - 例如run取决于quiz,或者当你make -j run和我&#39时你会感到惊讶; ve使用标准的$(RM)变量来实现可移植性。)