protocol-buffersprotobuf-c

Generate pb.c/pb.h files using protobuf-c without anonymous members in structs


I am trying to generate some pb.c and pb.h files using protobuf-c. I have earlier use nanopb to generate the same files but need to move to protobuf-c for a new project.

When generating the structure for a OneOf field, I see a difference in the generated files.

For the following definition in the proto file:

message MetricValue {
    oneof value {
        bool aBoolean = 1;
        string aString = 2;
        uint32 anInteger = 3;
        float aFloat = 4;
        double aDouble = 5;
    }
}

Nanopb generates the following:

typedef struct _MetricValue {
    pb_size_t which_value;
    union {
        bool aBoolean;
        char aString[32];
        uint32_t anInteger;
        float aFloat;
        double aDouble;
    } value;
/* @@protoc_insertion_point(struct:MetricValue) */
} MetricValue;

while using protobuf-c generates the following:

struct  MetricValue
{
  ProtobufCMessage base;
  MetricValue__ValueCase value_case;
  union {
    protobuf_c_boolean aboolean;
    char *astring;
    uint32_t aninteger;
    float afloat;
    double adouble;
  };
};

The way my project is configured, the build is unsuccessful with the following error: "An anonymous member in a struct is an extension to C (AnonymousMember)". I am aware that I can suppress this with some compile flags but the way the rest of the code is written, using an anonymous union will cause significant changes to my code.

Is there a way where I can force protobuf-c to not generate anonymous members?


Solution

  • Looking at the protobuf-c source code in c_message.cc, the generation of union {} is unconditional and has no option for naming it.