作者:Irises---_372 | 来源:互联网 | 2023-05-19 02:51
IhaveaMakefilesomethinglikethis:我有一个Makefile类似这样的东西:.SECONDARY:NVCCnvccNVCCFLAGS--
I have a Makefile something like this:
我有一个Makefile类似这样的东西:
.SECONDARY:
NVCC = nvcc
NVCCFLAGS = --gpu-architecture compute_20
CXX = g++
CXXFLAGS = -O3 -std=c++0x -Wall
CXXLINT = python cpplint.py
CXXLINTFLAGS = --filter=-build/include,-readability/streams,-runtime/sizeof,-whitespace/parens
PROTOC = protoc
PROTOCFLAGS = --cpp_out=.
BINS = my_binary
LIBS = -lcublas -lcusparse
PROTOS = $(wildcard *.proto)
SOURCES = $(wildcard *.cu)
HEADERS = $(wildcard *.cuh)
PBS = $(PROTOS:%.proto=%.pb)
DEPS = $(SOURCES:%.cu=%.d)
TESTS = my_test
all: lint protos
all: deps
all: bins
protos: ${PBS}
deps: ${DEPS}
bins: ${BINS}
clean:
rm -rf *.o *.d *.pb.cc *.pb.h ${BINS} ${TESTS}
lint:
${CXXLINT} ${CXXLINTFLAGS} ${SOURCES}
${CXXLINT} ${CXXLINTFLAGS} ${HEADERS}
tests: lint protos
tests: deps
tests: ${TESTS}
tests: tests-run
tests-run: ${TESTS}
for f in $^; do eval "/usr/bin/time -f \"$$f runtime: %E\" ./$$f"; done
%: %.o
${NVCC} ${NVCCFLAGS} -o $@ $^ ${LIBS}
%.d: %.cu
# ${CXXLINT} ${CXXLINTFLAGS} $*.cu
${NVCC} -M -o $*.d $*.cu
%.o: %.cu
${NVCC} ${NVCCFLAGS} -c -o $@ $*.cu
rm $*.d
%.pb: %.proto
${PROTOC} ${PROTOCFLAGS} $*.proto
${CXX} ${CXXFLAGS} -c -o $*.pb.o $*.pb.cc
ifneq ($(MAKECMDGOALS),clean)
-include ${DEPS}
endif
The problem occurs since I can not generate my DEPS until my proto target is built. Because building the protocol buffers will add a new header file to the tree, if this isn't done first before the DEPS, the nvcc -M (make dependency) will fail since it can not find the *.pb.h that is generated. Any ideas how to fix this?
问题出现了,因为在我的proto目标被构建之前,我不能生成我的DEPS。因为构建协议缓冲区将向树添加一个新的头文件,如果不是在DEPS之前先完成,nvcc -M (make dependency)将失败,因为它找不到*.pb。h生成。有办法解决这个问题吗?
2 个解决方案
6
Another solution is to make your dependency files depend on the results of the protocol buffer generation. The following snippet contains all steps to do that, since it is hard to explain them one by one, with an explanation of some items at the bottom:
另一种解决方案是使依赖文件依赖于协议缓冲区生成的结果。下面的代码片段包含了实现这一目的的所有步骤,因为很难逐个地对它们进行解释,在底部有一些项目的解释:
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 obtain compilation and linking flags relevant for the protobuf files. Of course, you have to add your own flags to this variable.
不需要pkg-config命令,但是如果您想要自动获得与protobuf文件相关的编译和链接标志,那么它是很方便的。当然,您必须向这个变量添加自己的标志。
The -xc++
is probably useless for you, but used here in order to be able to work with .cu
files and have them interpreted as C++; even for compilers different than nvcc
.
-xc++可能对您毫无用处,但在这里使用它是为了能够处理.cu文件并将其解释为c++;即使是与nvcc不同的编译器。
The line $(DEPS): $(PBOBJS)
is the indication that the protobuf files should be created and compiled before the dependencies are created. There a several ways to achieve this, so this is just an example of how to do it.
line $(DEPS): $(PBOBJS)是在创建依赖项之前应该创建和编译原始buf文件的指示。有几种方法可以做到这一点,这只是一个例子。
The .PRECIOUS
line instructs make
to keep the generated protobuf files. In this example snippet, those files are considered intermediate and as such would be deleted without this line.
宝贵的行指示保持生成的protobuf文件。在这个示例代码片段中,这些文件被视为中间文件,如果没有这一行,将会删除这些文件。
I posted this as a separate answer, because the previous one and this one do not have much in common.
我把这个作为一个单独的答案,因为前一个和这个没有太多的共同点。