$(CC) 用于编译 C 程序。$(CXX) 用于编译 C++ 程序。类似地,$(CFLAGS) 用于 C 程序,$(CXXFLAGS) 用于编译 C++。
将前几行更改为:
#CC = g++
LOADLIBES = -lm
CXXFLAGS = -Wall -O2 -g
(但请参阅其他人关于 -O2 和 -g 之间不兼容的注释。)
去掉这一行括号内的空格:
OBJS = $(SRC1:.cpp=.o)
将这些行更改为main
:
main: $(OBJS) $(SRC2)
# Built by implicit rules
生成的 makefile 应如下所示:
#CC = g++
LOADLIBES = -lm
CXXFLAGS = -Wall -O2 -g
SRC1 = Agent.cpp Breeder.cpp CandidateSolution.cpp \
Cupid.cpp FateAgent.cpp Grid.cpp Reaper.cpp \
fitness.cpp
SRC2 = main.cpp
SRC = $(SRC1) $(SRC2)
OBJS = $(SRC1:.cpp=.o)
AUX = $(SRC1:.c=.h)
main: $(OBJS) $(SRC2)
# Built by implicit rules
.PHONY: clean
clean:
rm -f *.o main
输出应如下所示:
$ make
g++ -Wall -O2 -g -c -o Agent.o Agent.cpp
g++ -Wall -O2 -g -c -o Breeder.o Breeder.cpp
g++ -Wall -O2 -g -c -o CandidateSolution.o CandidateSolution.cpp
g++ -Wall -O2 -g -c -o Cupid.o Cupid.cpp
g++ -Wall -O2 -g -c -o FateAgent.o FateAgent.cpp
g++ -Wall -O2 -g -c -o Grid.o Grid.cpp
g++ -Wall -O2 -g -c -o Reaper.o Reaper.cpp
g++ -Wall -O2 -g -c -o fitness.o fitness.cpp
g++ -Wall -O2 -g main.cpp Agent.o Breeder.o CandidateSolution.o Cupid.o FateAgent.o Grid.o Reaper.o fitness.o -lm -o main
为了完整起见,这是我在 Ubuntu 10.04 上使用的 make 版本:
$ make -v
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for i486-pc-linux-gnu