I need to use an existing C++ backed protobuf server (need to connect with unix socket) to receive protobuf requests. And I want to use golang as the protobuf client to send request to the server. However I found the autogenerated message definition in C++ and golang is different. There are much more definition in C++ version of protobuf in ::google::protobuf::Message. I use the following command to generate c++ protobuf definition based on the same proto definition:
protoc --proto_path=src --cpp_out=. src/sample.proto
protoc --proto_path=src --go_out=. src/sample.proto
But with the generated sample.pb.cc, sample.pb.h and sample.pb.go, the golang client message format is not recognized by the c++ server.
Is there a way to make this golang client c++ server protobuf communication work? Thanks.
============================ 2021.1.6 update: Add an example here:
Directory structure:
test
├── test.pb.cc
├── test.pb.go
├── test.pb.h
└── test.proto
proto file:
syntax = "proto2";
option go_package = "./;test";
package test;
message test {
required uint64 key = 1;
}
protoc commands to generate the test.pb.go, test.pb.h, test.ph.cc files:
protoc --proto_path=test --cpp_out=test test/test.proto
protoc --proto_path=test --go_out=test test/test.proto
Then you can see the golang generated data structure in test.pb.go:
type Test struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Key *uint64 `protobuf:"varint,1,req,name=key" json:"key,omitempty"`
}
C++ generated data structure:
class test : public ::google::protobuf::Message {
... // I'll ignore function definition
static const int kKeyFieldNumber = 1;
::google::protobuf::UnknownFieldSet _unknown_fields_;
::google::protobuf::uint64 key_;
mutable int _cached_size_;
::google::protobuf::uint32 _has_bits_[(1 + 31) / 32];
static test* default_instance_;
}
These two just seems not match...
For example, the _has_bits_
field kind of plays an important role in the c++ generated code, while there's no related logic in golang generated code.
// required uint64 key = 1;
inline bool test::has_key() const {
return (_has_bits_[0] & 0x00000001u) != 0;
}
inline void test::set_has_key() {
_has_bits_[0] |= 0x00000001u;
}
inline void test::clear_has_key() {
_has_bits_[0] &= ~0x00000001u;
}
inline void test::clear_key() {
key_ = GOOGLE_ULONGLONG(0);
clear_has_key();
}
inline ::google::protobuf::uint64 test::key() const {
return key_;
}
inline void test::set_key(::google::protobuf::uint64 value) {
set_has_key();
key_ = value;
}