2014-11-08 161 views
1

我是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; 
} 
+0

似乎编译器无法识别pc :: ts_service.pb.h中定义的:: pc :: TSResponse。编译器首先找到mob的ts_service.pb.h,它定义:: mob :: TSResponse。 – albertgu 2014-11-08 15:20:52

回答

1

听起来像问题是有两个不同的ts_service.proto文件冲突的定义。通常情况下,你可以通过将每个包的原型放在不同的目录中来解决这个问题。 pc/ts_service.protomob/ts_service.proto

请注意,使用protoc编译这些文件时,您需要设置导入路径以指向这两个目录的父目录;请勿将每个目录直接添加到路径中,因为这会导致相同的冲突。那就是:

# BAD 
protoc -Isrc/pc -Isrc/mob src/pc/cxs_service.proto 

# GOOD 
protoc -Isrc src/pc/cxs_service.proto 

注意,在每个.protoimport语句将进行更新,以使该文件的完整路径,进口,即import "/pc/ts_service.proto";而不是import "ts_service.proto";

+0

感谢您的建议。我可以成功编译这两个第三方原型。但是,当我将它们包含在我的应用程序中时,会发生编译错误 – albertgu 2014-11-09 01:50:07