Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading