我是Protocol Buffers(PB)的新手。现在我需要使用PB与2个第三方服务进行通信。 但它无法处理此编译错误:
cxs_service.pb.h:ISO C ++禁止声明
TSResponse' with no type cxs_service.pb.h: error: invalid use of
::'
我的标题文件包含2个第三方.h文件,如下所示:
#include "mob/include/ts_service.pb.h"
#include "pc/include/cxs_service.pb.h"
//### pc/include/cxs_service.pb.h ###
// The compiler seems to find ts_service.pb.h under pc/include successfully
// but it cannot recognize ::pc::TSResponse which is defined in it
# include "ts_service.pb.h"
namespace pc {
class CXSRequest : public ::google::protobuf::Message {
inline const ::pc::TSResponse& ts_response() const;
} // class CXSRequest
} // namespace pc
// i've found that mob/include/ts_service.pb.h, pc/include/ts_service.pb.h have the same header guard.
// Thus pc/include/cxs_service.pb.h really found pc/include/ts_service.pb.h.
// but cannot include it's content because of exactly the same header guard.
#ifndef PROTOBUF_ts_5fservice_2eproto__INCLUDED
#define PROTOBUF_ts_5fservice_2eproto__INCLUDED
#endif
第一个第三方PB消息:
// ts_service.proto
package mob;
message TSResponse {
required uint64 id = 1;
}
第二个第三方PB消息:
// cxs_service.proto
package pc;
import ts_service.proto;
message CXSRequest {
optional TSResponse ts_response = 1;
}
// which depends on its own ts_service.proto:
// ts_service.proto
package pc;
message TSResponse {
optional string name = 1;
}
答案 0 :(得分:1)
听起来问题是有两个不同的ts_service.proto
文件具有冲突的定义。通常你可以通过将每个包的protos放在不同的目录中来解决这个问题,例如: pc/ts_service.proto
和mob/ts_service.proto
。
请注意,使用protoc
编译这些文件时,您需要设置导入路径以指向这两个目录的父目录;不要直接将每个目录添加到路径,因为这会导致相同的冲突。那就是:
# BAD
protoc -Isrc/pc -Isrc/mob src/pc/cxs_service.proto
# GOOD
protoc -Isrc src/pc/cxs_service.proto
请注意,必须更新每个import
中的.proto
语句,以提供要导入的文件的完整路径,即import "/pc/ts_service.proto";
而不是import "ts_service.proto";
。< / p>