Wrapping Protobuf in an extra namespace

68 views
Skip to first unread message

Yakov Prager

unread,
Sep 17, 2020, 9:13:08 PM9/17/20
to Protocol Buffers

Hello friends,

My company uses Protobuf as a shared library.  I know that Google recommends linking Protobuf statically, especially if it is used by a shared library, but that is not an option in my case, for reasons that are beyond my control.  As a result of dynamic linking, I am encountering Protobuf versions clash, when a third-party software uses a different version of Protobuf than my company does.

In order to isolate the third-party software from "our" version of Protobuf, I would like to wrap our version into an additional namespace.  I found out that in the Protobuf file google/protobuf/port_def.inc, there exists a preprocessor symbols PROTOBUF_NAMESPACE, PROTOBUF_NAMESPACE_ID, and PROTOBUF_NAMESPACE_OPEN, which I could re-define.  For instance,

#define PROTOBUF_NAMESPACE_ID google::protobuf

can be replaced with

#define PROTOBUF_NAMESPACE_ID myNamespace::google::protobuf

I know that the above preprocessor symbol is _not_ consistently used through the Protobuf source files, but in principle it looks like it is not very hard to make the corresponding adjustments.

Has anybody tried that?  What were the difficulties you encountered?

Many thanks is advance!

-Yakov



Adam Cozzette

unread,
Sep 18, 2020, 11:26:38 AM9/18/20
to Yakov Prager, Protocol Buffers
I think this will work if you change the namespace everywhere, but the one caveat is that protobuf messages won't be interchangeable between the two protobuf versions. I.e. if you try to take a proto based on your myNamespace version and pass it to code that works with the version used by your third-party software, that will likely cause problems. If you are able to keep them separate then it should work, though.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/e174deee-c87e-4fd1-910c-2a22a707af58n%40googlegroups.com.

Yakov Prager

unread,
Sep 19, 2020, 11:52:18 AM9/19/20
to Protocol Buffers
Thank you very much for your answer.  You've also answered my second question, confirming my observation that there is a cyclical dependency between Protoc and some generated *.pb.cc and *.pb.h files -- in particular, descriptor.pb.c.  If I add an extra namespace around google::protobuf, how am I supposed to go about descriptor.pb.cc? -- Indeed, I need an updated version of descriptor.pb.cc in order to generate the updated protoc.exe, but how do I generate the new descriptor.pb.cc without first having the updated protoc.exe?

Many thanks in advance,

-Yakov
Reply all
Reply to author
Forward
0 new messages