[英]How do I convert a normal Makefile to create a static executable that can be used on another computer
[英]How do I make my makefile create a second executable?
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
当我运行 makefile 时,只有目标 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.