Manu Thomas Mathew
Manu Thomas Mathew

Reputation: 101

How to include .proto files having mutual dependency

I have two .proto files, which has two packages that have mutual dependency.

a.proto

syntax = "proto3";
import "b.proto";

package a;

message cert {
    string filename = 1;
    uint32 length = 2;
}

enum state {
    UP = 1;
    DOWN = 2;
}

message events {
    repeated b.event curevent = 1;
    uint32 val = 2;
}

b.proto

syntax = "proto3";
import "a.proto";

package b;

message event {
     a.cert certificate = 1;
     a.state curstate = 2;
}

When I try to generate cpp files, following error is seen

# protoc -I. --cpp_out=. b.proto b.proto: File recursively imports itself: b.proto -> a.proto -> b.proto

How can this be achieved ?

Note : protoc version used is libprotoc 3.3.0

Upvotes: 4

Views: 10672

Answers (1)

Ulas Keles
Ulas Keles

Reputation: 1781

proto compiler won't let you include circular dependencies. You will have to organize your code such that there aren't any recursive imports. One organization of your sample code above could be:

a.proto

syntax = "proto3";

package a;

message cert {
    string filename = 1;
    uint32 length = 2;
}

enum state {
    UNDEFINED = 0;
    UP = 1;
    DOWN = 2;
}

b.proto

syntax = "proto3";
import "a.proto";

package b;

message event {
    a.cert certificate = 1;
    a.state curstate = 2;
}

message events {
    repeated event curevent = 1;
    uint32 val = 2;
}

Your events type doesn't use anything from a.proto, and also uses event type from b.proto. It makes sense to move it to b.proto.

Upvotes: 1

Related Questions