日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

protobuf安裝

系統 1850 0
1 在網站 http://code.google.com/p/protobuf/downloads/list上可以下載 Protobuf 的源代碼。然后解壓編譯安裝便可以使用它了。
安裝步驟如下所示:
?
也可在https://github.com/google/protobuf/releases/tag/v3.0.0-alpha-3 上面下載這個,我是從上面的網站直接跳轉過來的找到的
?
提取 protobuf-3.0.0-alpha-3
?cd protobuf-3.0.0-alpha-3
sudo sh ./autogen.sh
?./configure --prefix=/usr/local/protobuf
?make?
?make check?
?make install?
?
?2 > sudo vim /etc/profile
?添加
export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/
保存執行
source /etc/profile
?
同時 在~/.profile中添加上面兩行代碼,否則會出現登錄用戶找不到protoc命令
?
3 > 配置動態鏈接庫路徑
sudo vim /etc/ld.so.conf
插入:
/usr/local/protobuf/lib
?
4 > su ?#root 權限
ldconfig
?

5> 寫消息文件:msg.proto

  1. package?lm;???
  2. message?helloworld???
  3. {???
  4. ????required?int32?????id?=?1;??//?ID?????
  5. ????required?string????str?=?2;??//?str????
  6. ????optional?int32?????opt?=?3;??//optional?field???
  7. }??
將消息文件msg.proto映射成cpp文件
protoc -I=. --cpp_out=. msg.proto
可以看到生成了
msg.pb.h 和msg.pb.cc
?
6> 寫序列化消息的進程
write.cc
  1. #include?"msg.pb.h"??
  2. #include?<fstream>??
  3. #include?<iostream>??
  4. using?namespace?std;??
  5. ??
  6. int?main(void)???
  7. {???
  8. ??
  9. ????lm::helloworld?msg1;???
  10. ????msg1.set_id(101);???
  11. ????msg1.set_str("hello");???
  12. ????fstream?output("./log",?ios::out?|?ios::trunc?|?ios::binary);???
  13. ??
  14. ????if?(!msg1.SerializeToOstream(&output))?{???
  15. ????????cerr?<<?"Failed?to?write?msg."?<<?endl;???
  16. ????????return?-1;???
  17. ????}??????????
  18. ????return?0;???
  19. }??
編譯 write.cc?
?g++ ?msg.pb.cc write.cc -o write ?`pkg-config --cflags --libs protobuf` -lpthread
?
執行write?
./write, 可以看到生成了log文件
?
7> 寫反序列化的進程
reader.cc
  1. #include?"msg.pb.h"??
  2. #include?<fstream>??
  3. #include?<iostream>??
  4. using?namespace?std;??
  5. ??
  6. void?ListMsg(const?lm::helloworld?&?msg)?{????
  7. ????cout?<<?msg.id()?<<?endl;???
  8. ????cout?<<?msg.str()?<<?endl;???
  9. }???
  10. ??
  11. int?main(int?argc,?char*?argv[])?{???
  12. ??
  13. ????lm::helloworld?msg1;???
  14. ??
  15. ????{???
  16. ????????fstream?input("./log",?ios::in?|?ios::binary);???
  17. ????????if?(!msg1.ParseFromIstream(&input))?{???
  18. ????????????cerr?<<?"Failed?to?parse?address?book."?<<?endl;???
  19. ????????????return?-1;???
  20. ????????}?????????
  21. ????}???
  22. ??
  23. ????ListMsg(msg1);???
  24. }??
編譯:g++ ?msg.pb.cc reader.cc -o reader ?`pkg-config --cflags --libs protobuf` -lpthread
執行./reader 輸出 :
101
hello

8> 寫Makefile文件

  1. all:?write?reader??
  2. ??
  3. clean:??
  4. ????rm?-f?write?reader?msg.*.cc?msg.*.h?*.o??log??
  5. ??
  6. proto_msg:??
  7. ????protoc?--cpp_out=.?msg.proto??
  8. ??
  9. ??
  10. write:?msg.pb.cc?write.cc??
  11. ????g++??msg.pb.cc?write.cc?-o?write??`pkg-config?--cflags?--libs?protobuf`??
  12. ??
  13. reader:?msg.pb.cc?reader.cc??
  14. ????g++??msg.pb.cc?reader.cc?-o?reader??`pkg-config?--cflags?--libs?protobuf`??

protobuf安裝


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 金昌市| 敖汉旗| 古丈县| 凤台县| 鄂尔多斯市| 远安县| 平南县| 清镇市| 南澳县| 巨野县| 辽宁省| 静安区| 易门县| 启东市| 甘洛县| 称多县| 常德市| 澄迈县| 西林县| 伊金霍洛旗| 广饶县| 无极县| 龙川县| 平遥县| 沙洋县| 都兰县| 开原市| 资兴市| 永泰县| 墨脱县| 时尚| 天水市| 桂阳县| 济阳县| 昌邑市| 烟台市| 宁波市| 林周县| 道真| 镇平县| 兖州市|