我是Protocol Buffers(PB)的新手。現在我需要使用PB與2個第三方服務進行通信。 但它失敗,此編譯錯誤的工作:協議緩衝區頭碰撞
cxs_service.pb.h: ISO C++ forbids declaration of
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;
}
似乎編譯器無法識別pc :: ts_service.pb.h中定義的:: pc :: TSResponse。編譯器首先找到mob的ts_service.pb.h,它定義:: mob :: TSResponse。 – albertgu 2014-11-08 15:20:52