热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Makefilew.协议缓冲区和自动依赖-Makefilew.ProtocolBufferandAutomaticDependency

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 个解决方案

#1


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.

我把这个作为一个单独的答案,因为前一个和这个没有太多的共同点。

#2


1  

When building the dependencies, you could opt to continue even if header files are missing. Using cpp or gcc, that can be achieved by using the options -MM -MG. I have no experience with NVCC, but I did not see any support those flags in the manual. If that is true, then you could try switching to cpp for the dependency generation only. From the cpp man page:

在构建依赖项时,您可以选择继续,即使头文件丢失。使用cpp或gcc,可以通过使用选项-MM -MG来实现。我没有使用NVCC的经验,但是我在手册中没有看到任何对这些标志的支持。如果这是正确的,那么您可以尝试仅为依赖项生成切换到cpp。cpp手册页:

-MG assumes missing header files are generated files and adds them to the dependency list without raising an error.

-MG假设丢失的头文件是生成的文件,并将它们添加到依赖列表中,不会引起错误。

Your pattern rule %.pb: %.proto does not seem right to me, by the way. It is %.pb.h and %.pb.cc that depend on %.proto and with the current approach, make will not know how to resolve any dependencies on %.pb.h because it will not know how to create the .pb.h file.

你的模式规则%。铅:%。顺便说一句,proto在我看来不太对。% .pb。h和% .pb。cc依赖于%。使用当前的方法,make将不知道如何解决对%.pb的任何依赖。因为它不知道如何创建。pb。h文件。


推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • 本文介绍了为什么要使用多进程处理TCP服务端,多进程的好处包括可靠性高和处理大量数据时速度快。然而,多进程不能共享进程空间,因此有一些变量不能共享。文章还提供了使用多进程实现TCP服务端的代码,并对代码进行了详细注释。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • 本文介绍了在mac环境下使用nginx配置nodejs代理服务器的步骤,包括安装nginx、创建目录和文件、配置代理的域名和日志记录等。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • phpcomposer 那个中文镜像是不是凉了 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
  • 如何利用 Myflash 解析 binlog ?
    本文主要介绍了对Myflash的测试,从准备测试环境到利用Myflash解析binl ... [详细]
  • 本文介绍了在Cpp中将字符串形式的数值转换为int或float等数值类型的方法,主要使用了strtol、strtod和strtoul函数。这些函数可以将以null结尾的字符串转换为long int、double或unsigned long类型的数值,且支持任意进制的字符串转换。相比之下,atoi函数只能转换十进制数值且没有错误返回。 ... [详细]
  • 使用C++编写程序实现增加或删除桌面的右键列表项
    本文介绍了使用C++编写程序实现增加或删除桌面的右键列表项的方法。首先通过操作注册表来实现增加或删除右键列表项的目的,然后使用管理注册表的函数来编写程序。文章详细介绍了使用的五种函数:RegCreateKey、RegSetValueEx、RegOpenKeyEx、RegDeleteKey和RegCloseKey,并给出了增加一项的函数写法。通过本文的方法,可以方便地自定义桌面的右键列表项。 ... [详细]
  • 本文介绍了解决java开源项目apache commons email简单使用报错的方法,包括使用正确的JAR包和正确的代码配置,以及相关参数的设置。详细介绍了如何使用apache commons email发送邮件。 ... [详细]
  • Question该提问来源于开源项目:react-native-device-info/react-native-device-info ... [详细]
author-avatar
Irises---_372
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有