all: exe1 exe2
exe1: obj1
g++ -Wall -Werror -std=c++11 program1.o -o program1 -lgtest
obj1:
g++ -c -Wall -Werror -std=c++11 program1.cc
exe2: obj2
g++ -Wall -Werror -std=c++11 program2.o -o program2
obj2:
g++ -c -Wall -Werror -std=c++11 program2.cc
clean:
rm *.o *.exe
当我运行生成文件时,仅目标exe1被编译并创建为可执行文件。如果我仅将目标设为exe2,则会收到错误消息
make: Nothing to be done for `all'.
如何使exe2对Makefile可见?
makefile可能看起来应该更像这样:
# Set the dependencies to the names of the executables you
# want to build
all: program1 program2
#
# Make uses some variables to define tools.
# The most important for you is CXX: Which is set to the C++ compiler.
#
# Notice that target name should match the executable name.
# This is because `make` will check its existence and creation date
# against its dependenc(ies) existence and creation date.
program1: program1.o
$(CXX) -Wall -Werror -std=c++11 program1.o -o program1 -lgtest
program2: program2.o
$(CXX) -Wall -Werror -std=c++11 program2.o -o program2
#
# We can generalize the creation of the object file.
%.o: %.cc
$(CXX) -c -Wall -Werror -std=c++11 $*.cc
clean:
$(RM) *.o *.exe
“ make”实用程序有许多规则可以简化标准构建。 Makefile的压缩版可以是:
PROGRAMS = program1 program2
all: $(PROGRAMS)
CXXFLAGS=-Wall -Werror -std=c++11
# Link program1 with the gtest library
program1: LDLIBS=-lgtest
clean:
$(RM) $(PROGRAMS) *.o
# Following section is needed only for binaries that need more than one object.
# Does not apply in the this case, since program1 depends only on program1.o.
# Included for allow extending the makefile.
program3: program3.o second.o # Additional objects here.