协议缓冲区的静态链接会导致与现有符号冲突

Static linking of Protocol Buffers results in conflicts with existing symbols

本文关键字:符号 冲突 缓冲区 静态 链接 协议      更新时间:2023-10-16

我们正在尝试为C++运行时实现协议缓冲区格式 (ONNX( 导入器。我们的运行时将由前端应用程序使用,这些应用程序也使用协议缓冲区模型。

当尝试执行同时运行前端和后端组件的进程时,我们看到错误,指示符号名称conflicts with the existing symbol

[libprotobuf ERROR google/protobuf/descriptor_database.cc:109] Symbol name "onnx.AttributeProto" conflicts with the existing symbol "onnx.AttributeProto".
[libprotobuf FATAL google/protobuf/descriptor.cc:1164] CHECK failed: generated_database_->Add(encoded_file_descriptor, size):
terminate called after throwing an instance of 'google::protobuf::FatalException'
what():  CHECK failed: generated_database_->Add(encoded_file_descriptor, size):
Aborted (core dumped)

有没有办法让两个组件(应用程序和共享对象(静态链接 PB 消息处理代码并在一个进程中注册相同的 Protobuf 符号?有没有办法告诉另一个组件不要重新注册PB消息?

命名空间

重整解决方案

协议缓冲区保存基于.proto文件名的全局注册表。当 2 个软件尝试将相同的 PB 消息添加到此注册表时,会出现名称冲突。

解决此问题的一种方法是人为地更改 PB 消息命名空间。您可以为 PB 消息定义不同的命名空间,并依靠 CMakefile 执行符号替换和重命名。

以下是onnx-tensorrt项目的示例:

https://github.com/onnx/onnx-tensorrt/blob/fa0964e8477fc004ee2f49ee77ffce0bf7f711a9/CMakeLists.txt#L92-L97

静态链接协议缓冲区

协议缓冲区保存基于.proto文件名的全局注册表。当 2 个软件尝试将相同的 PB 消息添加到此注册表时,会出现名称冲突。

解决此问题的一种方法是将整个协议缓冲区库静态链接到项目。这有效地创建了一个单独的注册表,可以在其中注册 PB 消息。

以下是ngraph项目的示例:

https://github.com/NervanaSystems/ngraph/commit/3d664abd7b8cc8295b6da79689f9bf119d55a2dd