<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    Google protobuf的安裝及使用

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接 [cpp] view plaincopy  
    1. 最近應為工作的需要,合作的部門提供了protobuf的接口,總結了一下使用的過程和方法如下:  
    2.   
    3.    
    4.   
    5.    
    6.   
    7. 下載protobuf-2.3.0:  
    8.     http://protobuf.googlecode.com/files/protobuf-2.3.0.zip  
    9. 安裝:   
    10. unzip protobuf-2.3.0.zip  
    11. cd protobuf-2.3.0  
    12. ./configure  
    13. make   
    14. make check   
    15. make install  
    16.   
    17. 結果:  
    18. Libraries have been installed in:  
    19.    /usr/local/lib  
    20. Head files hava been installed in:  
    21. /usr/local/include/google/  
    22. protobuf/  
    23.   
    24.   
    25. 開始寫.proto文件:  
    26. BaseMessage.proto:  
    27. message MessageBase  
    28. {  
    29.     required int32 opcode = 1;  
    30.     // other: sendMgrId, sendId, recvMgrId, recvId, ...  
    31. }  
    32.   
    33. message BaseMessage  
    34. {  
    35.     required MessageBase msgbase = 1;  
    36. }  
    37.   
    38. BaseMessage.proto是其它消息proto文件的基礎,以容器模塊的C2S_GetContainerInfo為例:  
    39. ContainerMessage.proto:  
    40. import "BaseMessage.proto";  
    41.   
    42. message C2SGetContainerInfoMsg  
    43. {  
    44.     required MessageBase msgbase = 1;  
    45.     optional int32 containerType = 2;  
    46. }  
    47.   
    48. .proto文件編寫規則:  
    49. 1)所有消息都需要包含msgbase這項,并編號都為1,即:  
    50.   required MessageBase msgbase = 1;  
    51. 2)除了msgbase這項寫成required外,其它所有項都寫成optional。  
    52.   
    53. 編譯 .proto 文件  
    54. protoc -I=. --cpp_out=. BaseMessage.proto  
    55. protoc -I=. --cpp_out=. ContainerMessage.proto  
    56. 生成BaseMessage.pb.h、BaseMessage.pb.cc  
    57.     ContainerMessage.pb.h、ContainerMessage.pb.cc  
    58. 將它們添加到工程文件中。  
    59.   
    60. 編寫C++代碼:  
    61. 1)發送消息:  
    62. C2SGetContainerInfoMsg msg;  
    63. msg.mutable_msgbase()->set_opcode(C2S_GetContainerInfo);  
    64. msg.set_containertype(1);  
    65. std::string out = msg.SerializeAsString();  
    66. send(sockfd, out.c_str(), out.size(), 0);  
    67. 2)接收消息  
    68. char buf[MAXBUF + 1];  
    69. int len;  
    70. bzero(buf, MAXBUF + 1);  
    71. len = recv(new_fd, buf, MAXBUF, 0);  
    72. if (len > 0)  
    73. {  
    74.     printf("%d接收消息成功:'%s',共%d個字節的數據/n",  
    75.             new_fd, buf, len);  
    76.   
    77.     BaseMessage baseMsg;  
    78.     std::string data = buf;  
    79.     baseMsg.ParseFromString(data);  
    80.   
    81.     int opcode = baseMsg.mutable_msgbase()->opcode();  
    82.   
    83.     printf("opcode=%d/n", opcode);  
    84.   
    85.     switch (opcode)  
    86.     {  
    87.     case C2S_GetContainerInfo:  
    88.     {  
    89.         C2SGetContainerInfoMsg msg;  
    90.         msg.ParseFromString(data);  
    91.   
    92.         printf("containerType=%d/n", msg.containertype());  
    93.   
    94.         break;  
    95.     }  
    96.     default:  
    97.     {  
    98.         break;  
    99.     }  
    100.     }  
    101. }  
    102. else  
    103. {  
    104.     if (len < 0)  
    105.         printf("消息接收失敗!錯誤代碼是%d,錯誤信息是'%s'/n",  
    106.              errno, strerror(errno));  
    107.     close(new_fd);  
    108.     return -1;  
    109. }  
    110.   
    111.   
    112. 編譯C++代碼:  
    113. Need to link lib:  
    114. protobuf  
    115. pthread  
    116.   
    117.   
    118. 參考:   
    119. 1,http://www.360doc.com/content/10/0822/16/11586_47942017.shtml  
    120. 2,http://code.google.com/p/protobuf/  
    121.   
    122.    

     

    原文地址:http://blog.csdn.net/ganghust/article/details/6115283

     

     

     

    項目主頁:http://code.google.com/p/protobuf/

    下載:http://code.google.com/p/protobuf/downloads/list protobuf-2.4.1.tar.gz

     

    1、./configure(注:默認可能會安裝在/usr/local目錄下,可以加--prefix=/usr來指定安裝到/usr/lib下,可以免去路徑的設置,路徑設置見Linux命令pkg-config

    2、make

    3、make check

    4、make install(需要超級用戶root權限)

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全