2014-11-08 157 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