我编写了一个简单的 Makefile 来编译几种类型的源代码。而且效果很好。这个Makefile会找到所有的protobuf源码和C++源码。因此,制作程序将如下所示。
protoc将生成C++源文件*.protog++*.cpp将使用and编译目标文件*.pb.cc(由步骤 1 生成)g++(实际上是ld)将所有对象链接*.obj到单个可执行文件这个 Makefile 示例是:
# Define the protoc generator
PROTOC = protoc
INCLUDE = -I$(SOMEPLACE)
LIB = -lxml2 -lpthread -lz
# The final single outputted executable
OUTPUT = svrkit_adapter_v2
# Define the cpp source code/object result
SOURCES = $(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
# Define the proto's source/generated result/object result
PROTO_SOURCES = $(wildcard *.proto)
PROTO_CPPSRC = $(patsubst %.proto,%.pb.cc,$(PROTO_SOURCES))
PROTO_OBJECT = $(patsubst %.proto,%.pb.o,$(PROTO_SOURCES))
# First actual making target
all:$(OUTPUT)
# Define the rule for generating proto's cpp sources
$(PROTO_CPPSRC) : $(PROTO_SOURCES)
$(PROTOC) --cpp_out=. $(filter %.proto, $^)
# Define the rule for compiling generated proto cpp sources
$(PROTO_OBJECT) : $(PROTO_CPPSRC)
$(CXX) $(CPPFLAGS) $(INCLUDE) -c $(filter %.cc, $^)
# Define the rule for compiling other cpp sources
$(OBJECTS): $(SOURCES) $(PROTO_OBJECT)
$(CXX) $(CPPFLAGS) $(INCLUDE) -c $(filter %.cpp, $^)
# Define the rule for linking the final executable
$(OUTPUT): $(OBJECTS) $(PROTO_OBJECT)
$(CXX) $(CFLAGS) -o $@ $^ ${LIB}
clean:
rm -f *.o *.~ *.bak *.pb.*
rm -f $(OUTPUT)
Run Code Online (Sandbox Code Playgroud)
然而make执行的命令行如下(例如):
protoc a.proto b.proto
g++ -c a.pb.cc b.pb.cc
g++ -c x.cpp y.cpp z.cpp
g++ -o output a.o b.o x.o y.o z.o
Run Code Online (Sandbox Code Playgroud)
有时效果很好。但如果源太多,重新编译一些未改变的源会花费大量时间。比如我刚刚修改了a.proto,Makefile也会重新编译b.proto。
我的问题是:如何使每个源文件单独编译/生成。执行的命令行应该是:
protoc a.proto
protoc b.proto
g++ -c a.pb.cc -o a.o
g++ -c b.pb.cc -o b.o
g++ -c x.cpp -o x.o
g++ -c y.cpp -o y.o
g++ -c z.cpp -o z.o
g++ -o output a.o b.o x.o y.o z.o
Run Code Online (Sandbox Code Playgroud)
先谢谢了。
您犯了一个错误,即让规则作用于完整列表而不是单个文件,从而剥夺了作用make于单个文件的能力。
你的生成规则:
# Define the rule for generating proto's cpp sources
$(PROTO_CPPSRC) : $(PROTO_SOURCES)
$(PROTOC) --cpp_out=. $(filter %.proto, $^)
# Define the rule for compiling generated proto cpp sources
$(PROTO_OBJECT) : $(PROTO_CPPSRC)
$(CXX) $(CPPFLAGS) $(INCLUDE) -c $(filter %.cc, $^)
Run Code Online (Sandbox Code Playgroud)
相反,使规则适用于单个文件。
# Define the rule for generating proto's cpp sources
%.pb.cc: %.proto
$(PROTOC) --cpp_out=. $<
# Define the rule for compiling generated proto cpp sources
%.pb.o: %.pb.cc
$(CXX) $(CPPFLAGS) $(INCLUDE) -c $< -o $@
Run Code Online (Sandbox Code Playgroud)
对于 C++ 源代码也是如此:
# Define the rule for compiling other cpp sources
$(OBJECTS): $(SOURCES) $(PROTO_OBJECT)
$(CXX) $(CPPFLAGS) $(INCLUDE) -c $(filter %.cpp, $^)
Run Code Online (Sandbox Code Playgroud)
(为什么$(PROTO_OBJECT)包含在依赖项中?这对我来说看起来非常错误。)
再次,将其切换为适用于单个文件而不是整个列表:
# Define the rule for compiling other cpp sources
%.o: %.cpp
$(CXX) $(CPPFLAGS) $(INCLUDE) -c $< -o $@
Run Code Online (Sandbox Code Playgroud)
只有最终目标需要完整的对象列表:
# Define the rule for linking the final executable
$(OUTPUT): $(OBJECTS) $(PROTO_OBJECT)
$(CXX) $(CFLAGS) -o $@ $^ ${LIB}
Run Code Online (Sandbox Code Playgroud)
扔掉PROTO_CPPSRC,不需要。
现在,make将查看最后一条规则,找到对象依赖关系,并将单文件规则应用于需要重建的每个对象。
与您的问题无关,但在您解决问题时,请将以下语句添加到您的 Makefile 中:
.PHONY: all clean
Run Code Online (Sandbox Code Playgroud)
这将使“all”和“clean”始终运行,即使存在具有该名称的文件。这通常不是什么问题,但当它发生时,它可能会令人困惑。;-)
| 归档时间: |
|
| 查看次数: |
948 次 |
| 最近记录: |