2
FLAGS:= -Wall -Wvla -g -lm
OBJECT := Nominated.h UniversityNominated.h AliceGraduate.h BobGraduate.h CollegeNominated.h ColinGraduate.h DannyGraduate.h NominatedList.h Parser.h Parser.cpp
all: Hire

Nominated.o: Nominated.h Nominated.cpp
    g++ -c $(FLAGS) Nominated.cpp -o Nominated.o

NominatedList.o: Nominated.h NominatedList.h NominatedList.cpp
    g++ -c $(FLAGS) NominatedList.cpp -o NominatedList.o

UniversityNominated.o: Nominated.h UniversityNominated.h UniversityNominated.cpp
    g++ -c $(FLAGS) UniversityNominated.cpp -o UniversityNominated.o

AliceGraduate.o: Nominated.h UniversityNominated.h AliceGraduate.h AliceGraduate.cpp
    g++ -c $(FLAGS) AliceGraduate.cpp -o AliceGraduate.o

BobGraduate.o: Nominated.h UniversityNominated.h BobGraduate.h BobGraduate.cpp
    g++ -c $(FLAGS) BobGraduate.cpp -o BobGraduate.o

CollegeNominated.o: Nominated.h CollegeNominated.h CollegeNominated.cpp
    g++ -c $(FLAGS) CollegeNominated.cpp -o CollegeNominated.o

ColinGraduate.o: Nominated.h CollegeNominated.h ColinGraduate.h ColinGraduate.cpp
    g++ -c $(FLAGS) ColinGraduate.cpp -o ColinGraduate.o

DannyGraduate.o: Nominated.h CollegeNominated.h DannyGraduate.h DannyGraduate.cpp
    g++ -c $(FLAGS) DannyGraduate.cpp -o DannyGraduate.o

Parser.o: $(OBJECT)  
    g++ -c $(FLAGS) Parser.cpp -o Parser.o

Parser: Nominated.o UniversityNominated.o AliceGraduate.o BobGraduate.o CollegeNominated.o ColinGraduate.o DannyGraduate.o NominatedList.o Parser.o
    ar rcu libParser.a Nominated.o UniversityNominated.o AliceGraduate.o BobGraduate.o CollegeNominated.o ColinGraduate.o DannyGraduate.o NominatedList.o Parser.o
    ranlib libParser.a

Hire: Hire.cpp Parser
    g++ Hire.cpp libParser.a -o Hire

clean:
    rm -f Hire *.a *.o *~
.PHONY: clean all

所以这是我的 Makefile。

我怎样才能不那么难看?

我总是读到这一点,但我不明白

如何 ?

我的意思是我知道我需要使用通配符工具,但正如你一样,我可能用错了

4

3 回答 3

4

以下是如何使用通配符和模式替换的示例:

SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp,%.o,$(SOURCES))
CXX := g++ -Wall -Wvla -g

.cpp.o:
        ${CXX} -c $<

这会将所有 .cpp 文件构建到它们的目标文件中。

于 2013-01-03T19:15:32.353 回答
4

您可以做很多事情,但最好的事情之一是针对特定类型的模式规则。您的所有对象似乎都是以相同的方式构建的。

%.o: %.cpp
    ${CXX} -c $(FLAGS) -o $@ $<

您会注意到这不包含任何头文件作为依赖项。手动维护 Makefile 中的头文件依赖项是一个坏主意,因为在您修改程序时它必然会过时。有时您的编译器可以自动为您生成友好的依赖项。假设您有一个对象列表,例如:

OBJS := $(SOURCES:%.c=%.o)

您可以在 Makefile 的最后包含它(之后没有空行)

-include $(OBJS:.o=.d)

然后将,添加-MMD到您的编译器标志。这将导致 GCC 生成 .d 文件,其中包含每个对象的标头依赖项作为 Make 规则,并且上面的 include 行将包含这些规则并使用它们来解决对象的依赖项。这意味着任何标头更改都将导致包含它的所有文件(直接或间接)被重建。

于 2013-01-03T19:24:43.867 回答
2

尝试这个:

FLAGS   = -Wall -Wextra
SRC     = $(wildcard *.cpp)
OBJ     = $(patsubst %.cpp,%.o,$(SRC))
DEP     = $(patsubst %.cpp,%.d,$(SRC))
PARSER  = $(filter-out Hire.o,$(OBJ))

all: $(DEP) build

build:
    make Hire

%.d:
    g++ -MM $*.cpp > $*.d

%.o: %.cpp
    g++ $(FLAGS) -c $*.cpp

libParser.a: $(PARSER)
    ar rcu libParser.a $(PARSER)
    ranlib libParser.a

Hire: Hire.cpp libParser.a
    g++ Hire.cpp libParser.a -o Hire

clean:
    rm -f Hire *.a *.o *.d *~

.PHONY: clean all 

#
# This line includes all the dependencies.
# There is one for each file.
# Check the *.d files after you run make
-include $(DEP)
于 2013-01-03T21:37:15.627 回答