编译含Protobuf map字段的CUDA文件时出现返回类型不兼容错误
CUDA编译含gRPC/Protobuf map字段的项目报错
问题背景
我正在编译一个使用CUDA、gRPC和Protobuf的项目。该项目最初基于gRPC 1.20和Protobuf 3.7构建,现在尝试升级到最新版本的gRPC(包括1.55、1.56.x及master分支),但编译时遇到错误,问题出在Protobuf消息中的map<string,string>字段。
最小复现示例(MRE)
foo.proto
syntax = "proto3"; package Foo; message Bar { map<string,string> baz = 1; } message Result {} service FooService { rpc FooFunc (Bar) returns (Result) {} }
main.cu
#include "cpp/foo.pb.h" int main() { return 0; }
编译步骤与错误
- 编译Protobuf文件(使用完整路径确保版本一致):
/usr/local/bin/protoc --grpc_out=cpp --cpp_out=cpp --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin foo.proto
- 以常规C++文件编译
main.cu,编译成功:
nvcc -x cpp main.cu -I/usr/local/include -L/usr/local/lib -lprotoc -lcuda -lprotobuf -lgrpc++ -lgrpc -std=c++17
- 以CUDA C++文件编译
main.cu,出现以下错误:
/usr/local/include/google/protobuf/map_field.h(708): error: return type is not identical to nor covariant with return type "google::protobuf::internal::UntypedMapBase *" of overridden virtual function "google::protobuf::internal::MapFieldBaseForParse::MutableMap" /usr/local/include/google/protobuf/map_field.h(626): error: return type is not identical to nor covariant with return type "google::protobuf::internal::UntypedMapBase *" of overridden virtual function "google::protobuf::internal::MapFieldBaseForParse::MutableMap" detected during instantiation of class "google::protobuf::internal::MapField<Derived, Key, T, kKeyFieldType_, kValueFieldType_> [with Derived=Foo::Bar_BazEntry_DoNotUse, Key=std::string, T=std::string, kKeyFieldType_=google::protobuf::internal::WireFormatLite::TYPE_STRING, kValueFieldType_=google::protobuf::internal::WireFormatLite::TYPE_STRING]" cpp/foo.pb.h(272): here 2 errors detected in the compilation of "main.cu".
已尝试的解决方案
- 测试多个gRPC版本:1.55、1.56.x及master分支,均出现相同错误
- 更换Protobuf/gRPC的安装路径:/usr/local、$HOME/.local、$HOME/.grpc,问题依旧
- 运行
nvcc main.cu --keep查看预编译文件,仅生成main.cpp4.ii,未生成main.cpp1.ii,推测错误来自CUDA代码的C++预处理器阶段
临时解决办法
使用旧版本的gRPC(v1.50和v1.54均可)可以成功编译项目,推测gRPC 1.54之后的版本改动导致NVCC在CUDA编译模式下报错。
内容的提问来源于stack exchange,提问作者ysan
相关产品推荐
相关产品推荐

