如何将 protobuf 文件从一个文件夹导入 Eclipse 中的另一个文件夹?

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

我有一个包含 google 协议缓冲区的现有 eclipse 项目。我正在尝试在新文件夹中添加新的 .proto,然后将其从 .proto 包含在原始文件夹中。

当我尝试构建它时,我得到:

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

如何在 eclipse 的不同文件夹中引用另一个 .proto?如果我使用绝对文件路径,那么该项目将不可移植。

如果我只是在没有路径的情况下导入“PanicShared.proto”,那么导入行本身不会出错,但是在 PanicShared.proto 中我有:

enum PanicLevel {
    NORMAL = 0;
    etc.
}

当我尝试在另一条消息中使用它时:

import "PanicShared.proto";

message PanicPremium {
  repeated PanicLevel panicPremiumLevels = 11;
}

我收到一个错误:

[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.

我通常通过相对包含解决了这个问题。

如果这是我的结构:

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

我希望 file1.proto 包含 file2.proto 我愿意:

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

在 file1 中它会说:

import "file2.proto";

这是在我的机器上完成的示例:

$ 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

实际上有两个问题导致失败。

首先是我需要将文件添加到包含路径,然后只使用文件的 non-qualified 名称(即只是 "PanicShared.proto")。

第二个是出于某种原因我需要限定对象的名称以便从该文件中引用它们。即 shared.proto.PanicPremiumLevel 而不是 PanicPremiumLevel.