google protobuf學習筆記一:windows下環境配置

來源:互聯網
上載者:User

標籤:windows   環境配置   google protobuf   protocol buffers   

歡迎轉載,轉載請註明原文地址:http://blog.csdn.net/majianfei1023/article/details/45371743


protobuf的使用和原理,請查看:http://blog.csdn.net/majianfei1023/article/details/45112415

Windows下google protobuf開發環境配置

最近項目需求,Client與Server的網路通訊協定傳輸使用google protobuf rpc。對於Protobuf,以前是只知道有這個東西,知道它幹嘛,而且一直很仰慕,但是從來沒學習過,最近剛好趁著項目機會,好好學習學習。

這篇主要講,在windows下用vs2012配置google protobuf開發環境。

一.準備工作:

1.Windows7或其他Windows系統.


2.Visual Studio 2012或其他版本VS.


3.protobuf:我使用的是最新版本的protobuf(protobuf-2.6.1)。

windows下,需要下載兩個包:

protobuf-2.6.1.tar.bz2 (protobuf所有的源碼都在裡面,用它進行編譯成lib庫)

protoc-2.6.1-win32.zip(內涵protoc.exe,用來把proto檔案編譯成目標語言(C++,Java,Python)的文本,是google protobuf定義的格式。其實這個檔案不是必須的,編譯protobuf-2.6.1.tar.bz2的時候會產生這個exe)

去官網下載protobuf的source code和編譯工具:https://developers.google.com/protocol-buffers/docs/downloads


然後解壓:我本地的路徑是D:\protobuf-2.6.1


二.配置環境:


1.進入 D:\protobuf-2.6.1\vsprojects,用vs2012運行protobuf.sln,然後build solution。

這些大家都會,我就不羅嗦了。

然後你就會發現,D:\protobuf-2.6.1\vsprojects\Debug目錄下出現了很多內容,最重要的幾個就是:libprotobuf.lib,libprotobuf-lite.lib,libprotoc.lib和protoc.exe


2.D:\protobuf-2.6.1\examples本來是有例子的,我們親自實踐下,動手自己在此目錄下定義一個proto:

package tutorial;message Person {  required string name = 1;  required int32 age = 2;  optional string email = 3;}
然後使用cmd運行protoc.exe產生我們的目標語言格式(c++).

cd D:\protobuf-2.6.1\vsprojects\Debug

D:\protobuf-2.6.1\vsprojects\Debug>protoc -I=D:\protobuf-2.6.1\examples --cpp_out=D:\protobuf-2.6.1\examples D:\protobuf-2.6.1\examples\person.proto

然後可以看到,產生了person.pb.h和person.pb.cc的檔案。

3.我們用vs2012建立一個空的項目,選擇屬性,配置一下:



點擊 配置屬性 下的 C/C++ 的 常規,右邊附加元件封裝含目錄,匯入這個路徑D:\protobuf-2.6.1\src

點擊連結器 的常規,右邊的附加庫目錄,匯入這個路徑D:\protobuf-2.6.1\vsprojects\Debug



三.開始一個最簡單的項目

好了,一切配置好了,該寫代碼了,我們做一個最簡單的輸入輸出。建立一個main.cpp,然後把之前產生的person.pb.h和person.pb.cc複製到項目裡面,並添加到項目裡面。

#include <iostream>#include "person.pb.h"using namespace std;using namespace tutorial;int main(){Person person;person.set_name("flamingo");   person.set_age(18);   cout<<person.name()<<endl;cout<<person.age()<<endl;system("pause");return 0;}


有些人說可以正常運行,但是我這邊不行,主要是


網上尋找原因,終於發現,需要在代碼裡面加兩行:


#pragma comment(lib, "libprotobuf.lib")#pragma comment(lib, "libprotoc.lib")


就能正常跑了:



protobuf的使用和原理,請查看:http://blog.csdn.net/majianfei1023/article/details/45112415

google protobuf學習筆記一:windows下環境配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.