$(CC)用于编译C程序。 $(CXX)用于编译C++程序。同样,$(CFLAGS)用于C程序,$(CXXFLAGS)用于编译C++。
更改的前几行这样的:
#CC = g++
LOADLIBES = -lm
CXXFLAGS = -Wall -O2 -g
(但是看别人的笔记约-02和-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 -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
这q uestion与C++无关,因此我删除了[C++]标签 –
根据Robᵩ的说法,它的确如此 –
这个问题是关于如何使用Makefile。在这个关于C++语言的问题中没有任何东西。你碰巧使用make来构建一个用C++编写的程序,但它用C++编写的事实是正交的。你的代码可能已经写在FORTRAN中。 –