Another solution is to make your dependency files depend on the results of the protocol buffer generation. The following snippet contains all the steps for this, since it is difficult to explain them one at a time, explaining some of the elements below:
CXX_FLAGS := $(shell pkg-config --cflags protobuf) -xc++ LD_FLAGS := $(shell pkg-config --libs protobuf) PROTOS := $(wildcard *.proto) PROTO_OBJS := $(PROTOS:.proto=.pb.o) BINS := my_binary SRCS := $(wildcard *.cu) OBJS := $(SRCS:.cu=.o) DEPS := $(SRCS:.cu=.d) PBSRCS := $(wildcard *.proto) PBOBJS := $(PROTOS:.proto=.pb.o) PBGENS := $(PROTOS:.proto=.pb.cc) $(PROTOS:.proto=.pb.h) all: $(BINS) clean: rm -f $(BINS) $(OBJS) $(DEPS) $(PBOBJS) $(PBGENS) $(BINS): $(OBJS) $(OBJS): $(DEPS) $(DEPS): $(PBOBJS) .PRECIOUS: $(PBGENS) %.d: %.cu $(CXX) -M $(CXX_FLAGS) $< > $@ %.pb.cc: %.proto protoc --cpp_out=. $< %.pb.o : %.pb.cc $(CXX) $(CXX_FLAGS) -c -o $@ $< %.o: %.cu $(CXX) $(CXX_FLAGS) -c -o $@ $< $(BINS): %: %.o $(CXX) $(LD_FLAGS) -o $@ $(PROTO_OBJS) $^ ifneq ($(MAKECMDGOALS),clean) -include $(DEPS) endif
The pkg-config command is not required, but is convenient if you want to automatically get compilation and link flags corresponding to protobuf files. Of course, you must add your own flags to this variable.
-xc++ is probably useless for you, but it is used here to be able to work with .cu and interpret them as C ++; even for compilers other than nvcc .
The line $(DEPS): $(PBOBJS) is an indication that protobuf files must be created and compiled before dependencies are created. There are several ways to achieve this, so this is just an example of how to do this.
The .PRECIOUS line tells make to save the generated protobuf files. In this example fragment these files are considered intermediate and as such will be deleted without this line.
I posted this as a separate answer because the previous one and this one do not have much in common.
source share