Protocol Buffers. Error in generated header file via proto-c. Invalid conversion

82 views Asked by At

In my project C/C++ I must using Protocol Buffer with protoc file. After generated code from file switch.proto using below tool

protoc-c -I="./proto/" --c_out="./protobuf-c" proto/*.pro

I get files. Header switch.pb-c.h and source file switch.pb-c.c. The problem is in generated code in header file which give me error


error: invalid conversion from 'int' to 'SwitchValue' {aka '_SwitchValue'} [-fpermissive]
         , 0,0 }

This error I found in code in header file each I shouldn't edit:

#define SWITCH_REPORT__INIT \
 { PROTOBUF_C_MESSAGE_INIT (&switch_report__descriptor) \
    , 0,0 }

syntax = "proto3";

import "metadata.proto";

message SwitchRequest {
  DeviceMetadata metadata = 1;
}

message SwitchResponse {}

message SwitchReport {
  SwitchValue value = 1;
}

enum SwitchValue {
  SWITCH_UNKNOWN = 0;
  SWITCH_ON = 1;
  SWITCH_OFF = 2;
}

For now I don't know how it solve. One suspect one thing, is to old gcc compiler 8.3.0. If I'm right, I also will be must upgrade my system (actualy Debian 9.8) and this is will be not good.

1

There are 1 answers

1
jpa On BEST ANSWER

error: invalid conversion from 'int' to 'SwitchValue' {aka '_SwitchValue'} [-fpermissive] , 0,0 }

The header file is trying to initialize an enum type with a value of 0. This is valid thing to do in C, but not allowed by default in C++.

If you are using C++ in your project, you should probably use Google's official C++ protobuf library.

But if for some reason you need to use the protobuf-c library in a C++ project, add -fpermissive to the compilation options. This will allow the code to compile, but it will also disable some other useful compiler errors.