由于在網上隻能找到 可執行檔案和源碼,而沒有相應的.lib或.a 于是我這裡就自己搞一下了。
下載下傳源碼:點選打開連結
我是用的是cpp 即c++源碼版本編譯
首先我們先進入protobuf-3.1.0\cmake目錄,檢視README說明就知道如何編譯windows版本了。
準備:
確定你的系統有CMake 點選官網下載下傳 ,Visual Studio ,(Git是非必須的)。
開始:
通過開始菜單,打開“VS2015 x64 Native Tools Command Prompt” 我目前使用的是2015版本。
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC> 切換到你的工作目錄我是用指令:
E:
E:\> cd study\protobuf\protobuf-cpp-3.1.0
E:\study\protobuf\protobuf-cpp-3.1.0> mkdir install 建立一個存放build出來結果的目錄
進入protobuf目錄 進入cmake目錄
E:\study\protobuf\protobuf-cpp-3.1.0\protobuf-3.1.0\cmake>mkdir build & cd build
E:\study\protobuf\protobuf-cpp-3.1.0\protobuf-3.1.0\cmake\build>mkdir release & cd release
接着使用指令:
cmake -G "NMake Makefiles" ^
-DCMAKE_BUILD_TYPE=Release ^
-DCMAKE_INSTALL_PREFIX=../../../../install ^
../..
這是建立release版本同樣傳回 上一層目錄 build
E:\study\protobuf\protobuf-cpp-3.1.0\protobuf-3.1.0\cmake\build>mkdir debug & cd debug
建立debug版本
cmake -G "NMake Makefiles" ^
-DCMAKE_BUILD_TYPE=Debug ^
-DCMAKE_INSTALL_PREFIX=../../../../install ^
../..
接着在傳回上一層目錄 建立解決方案
E:\study\protobuf\protobuf-cpp-3.1.0\protobuf-3.1.0\cmake\build>mkdir solution & cd solution
cmake -G "Visual Studio 14 2015 Win64" ^
-DCMAKE_INSTALL_PREFIX=../../../../install ^
../..
我是用的是 vs2015 這個根據你自己的版本修改就行了。
進入到solution目錄你就會發現已經産生了一個 protobuf.sln 的工程檔案,沒錯這個就是我們需要的工程
編譯方式有兩種:
1、 就是直接進入build\release目錄使用指令 nmake 就開始編譯了(debug版本同理)。
2、通過vs IDE打開剛才的工程檔案,選擇release或debug版本 開始編譯就可以了。
編譯完成後(其實這時候你進入release目錄就可以看到已經編譯出來了,)
就需要進行test測試了
看到pass 測試通過後
執行 nmake install安裝(其實我的測試沒有通過,錯誤是因為64編譯出現問題,于是我是用sln工程檔案在ide中編譯)
完後可以看到 release版本有 libprotobuf.lib libprotoc.lib protoc.exe 檔案
那麼我們就可以寫proto檔案 通過 cmd終端執行
>protoc.exe --proto_path=./ --cpp_out=./output myMessage.proto
--proto_path= 指定proto檔案目錄
--cpp_out=指定輸出cpp檔案目錄
myMessage.proto 為proto源檔案名
注意我是用的3版本proto檔案的書寫需要加上 syntax="proto2"; 指定使用哪個版本
我的myMessage.proto 檔案内容如下
syntax="proto2";
message LogonReqMessage {
required int64 acctID = 1;
required string passwd = 2;
}
加入使用proto3 required字段不再允許了
加入使用3可以按照以下寫法
syntax="proto3";
message LogonReqMessage {
int64 acctID = 1;
string passwd = 2;
}
是不是簡單多了,這個就是3的功能,至于3的變化有哪些請檢視官方說明 https://github.com/google/protobuf/releases
還有為了友善我們最後自己寫一個bat腳本, 當修改了proto後 直接點選執行bat檔案就可以完事,不需要每次進入相應目錄執行 proto.exe 指令那麼麻煩。
貼出我編譯的結果連結,大家可以前往下載下傳:點選打開連結