Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import protobuf files from one folder in another folder in Eclipse?

I have an existing eclipse project including google protocol buffers. I'm trying to add a new .proto in a new folder and then include it from a .proto in the original folder.

When I try to build this I get:

..\shared\PanicShared.proto: Backslashes, consecutive slashes, ".", or ".." are not allowed in the virtual path

How do I reference another .proto in a different folder in eclipse? If I use an absolute file path then the project will not be portable.

If I just import "PanicShared.proto" without the path then the import line itself does not error however inside PanicShared.proto I have:

enum PanicLevel {
    NORMAL = 0;
    etc.
}

When I try to use that in another message though:

import "PanicShared.proto";

message PanicPremium {
  repeated PanicLevel panicPremiumLevels = 11;
}

I get an error:

[protoc] PanicPremium.proto:9:12: "PanicLevel" is not defined.

[protoc] [libprotobuf WARNING google/protobuf/descriptor.cc:5411] Warning: >Unused import: "PanicPremium.proto" imports "PanicShared.proto" which is not used.

like image 887
Tim B Avatar asked Jan 07 '16 15:01

Tim B


People also ask

What is the Protobuf file format?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

How do I open a Protobuf file?

If you cannot open your PROTO file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a PROTO file directly in the browser: Just drag the file onto this browser window and drop it.

How do I open a folder in eclipse?

Navigate to the folder of the exported file. Select the file and click 'Open'. In the 'Import Projects' dialog, ensure that browsed path is displayed. Click 'Finish'.


2 Answers

I have normally solved this problem by doing relative includes.

So if this is my structure:

  project
   +- dir1
      +- file1.proto
   +- dir2
      +- file2.proto

and I want file1.proto to include file2.proto I do:

protoc -I ../dir2 <other args you need> file1.proto

And in file1 it will say:

import "file2.proto";

Here is a worked through example on my machine:

$ find `pwd` -type f
/tmp/so/shared/PanicShared.proto
/tmp/so/main/Main.proto

$ cat /tmp/so/shared/PanicShared.proto
enum PanicLevel {
    NORMAL = 0;
}
$ cat /tmp/so/main/Main.proto
import "PanicShared.proto";

message PanicPremium {
  repeated PanicLevel panicPremiumLevels = 11;
}

$ cd /tmp/so/shared

$ protoc -I . PanicShared.proto --cpp_out=.

$ g++ PanicShared.pb.cc -c -o PanicShared.pb.o

$ cd /tmp/so/main

$ protoc -I . -I ../shared Main.proto --cpp_out=.

$ g++ Main.pb.cc -c -o Main.pb.o -I ../shared

$ protoc --version
libprotoc 2.5.0

$ cd /tmp/so/

$ find `pwd` -type f
/tmp/so/shared/PanicShared.pb.cc
/tmp/so/shared/PanicShared.proto
/tmp/so/shared/PanicShared.pb.h
/tmp/so/shared/PanicShared.pb.o
/tmp/so/main/Main.pb.o
/tmp/so/main/Main.pb.h
/tmp/so/main/Main.proto
/tmp/so/main/Main.pb.cc
like image 191
Jonah Graham Avatar answered Sep 22 '22 23:09

Jonah Graham


There were actually two problems causing this to fail.

The first was that I needed to add the file to the include path and then just use the non-qualified name for the file (i.e. just "PanicShared.proto").

The second was that for some reason I needed to qualify the names of the objects to reference them from that file. i.e. shared.proto.PanicPremiumLevel not PanicPremiumLevel.

like image 25
Tim B Avatar answered Sep 21 '22 23:09

Tim B