所需工具 : cmake for windows 和 git for windows
原理:protobuf 是google的一個開源項目,其源代碼在github上可以下載到,并且源碼都采用cmake來構建,所以我們可以把源碼下載到本地,然后了利用cmake構建本地工程,然后編譯.
步驟一:下載源碼
復制以下代碼,保存到 download_protobuf_source.bat 文件中,運行即可
::參考文章 https://github.com/google/protobuf/blob/master/cmake/README.md::默認當前操作系統(tǒng)已安裝 git 和 cmake,并配置好了環(huán)境變量echo off & color 0A::設置所需要的Protobuf版本,最新版本可以在github上查到 https://github.com/google/protobufset PROTOBUF_VESION="3.0.0-beta-4"echo %PROTOBUF_VESION%set PROTOBUF_PATH="protobuf_%PROTOBUF_VESION%"echo %PROTOBUF_PATH%::從githug上拉取protobuf源代碼git clone -b %PROTOBUF_VESION% https://github.com/google/protobuf.git %PROTOBUF_PATH%::從github上拉取gmockcd %PROTOBUF_PATH%git clone -b release-1.7.0 https://github.com/google/googlemock.git gmock::從github上拉取gtestcd gmockgit clone -b release-1.7.0 https://github.com/google/googletest.git gtestpause
步驟二:編譯
你可以利用cmake構建你所需要的版本,下面的的例子是構建并編譯一個VS2013版本的protobuf
例:構建VS2013版本
復制以下代碼,保存到 build_VS.bat 文件,放到 download_protobuf_source.bat 同級目錄,然后執(zhí)行
例如
::參考文章 https://github.com/google/protobuf/blob/master/cmake/README.md::默認當前操作系統(tǒng)已安裝 git 和 cmake,并配置好了環(huán)境變量echo off & color 0A::設置所需要的Protobuf版本,最新版本可以在github上查到 https://github.com/google/protobuf::必須與下載的版本一致set PROTOBUF_VESION="3.0.0-beta-4"echo %PROTOBUF_VESION%set PROTOBUF_PATH="protobuf_%PROTOBUF_VESION%"echo %PROTOBUF_PATH%cd %PROTOBUF_PATH%::設置VS工具集,相當于指定VS版本,取決于VS的安裝路徑set VS_DEV_CMD="D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat"::設置工程文件夾名字,用來區(qū)分不同的VS版本set BUILD_PATH="build_VS2013"::設置編譯版本 Debug Or Releaseset MODE="Release"cd cmakeif not exist %BUILD_PATH% md %BUILD_PATH%cd %BUILD_PATH%if not exist %MODE% md %MODE%cd %MODE%::開始構建和編譯call %VS_DEV_CMD%cmake ../../ -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=%MODE%call extract_includes.batnmake /f Makefileecho %cd%pause
當然,你也可以的通過修改上面的的腳本來編譯你所需要的VS版本,具體的參數(shù)注釋的很詳細
當進度達到100%的時候,說明編譯完成
此時,所有的東西都已經(jīng)生成,包括頭文件 和 lib文件
測試
新建VS2013工程,設置好google/protobuf 頭文件目錄 和 lib庫目錄,鏈接到 libprotobuf.lib 或者 libprotobuf-lite.lib ,此處不贅述.
新建 protocol.proto 文件,輸入以下協(xié)議,需要注意的是,一定要加入 syntax = "proto2" 指定語法規(guī)則版本,否則在執(zhí)行 protoc.exe 的過程中會報警告.如果不加也沒有影響,默認為 proto2 的語法規(guī)則
// 指定語法規(guī)則 proto2 or proto3syntax = "proto2";message Book{ optional string name = 1; optional int32 pages = 2; optional float price = 3;}message Student{ optional int32 age = 1; optional string name = 2; optional float score = 3; repeated Book arrBook = 4;}
新建生成協(xié)議腳本 gen.bat ,輸入以下內容
@echo off & color 0A:: protoc程序名set "PROTOC_EXE=protoc.exe":: .proto文件名set "PROTOC_FILE_NAME=protocol.proto"set "PROTOC_PATH=%cd%"set "CPP_OUT_PATH=%cd%"::生成.h和.cc"%PROTOC_PATH%\%PROTOC_EXE%" --proto_path="%PROTOC_PATH%" --cpp_out="%CPP_OUT_PATH%" "%PROTOC_PATH%\%PROTOC_FILE_NAME%"pause
把生成的 protocol.pb.h 和 protocol.pb.cc 加入到剛才的工程
例如
輸入代碼:
#include <stdio.h>#include <stdint.h>#include "protocol.pb.h"int32_t main(){ Student *student1 = new Student(); student1->set_age(1); student1->set_name("tom"); student1->set_score(98.5); for (uint32_t i = 0; i < 5; ++i) { char name[32] = { 0 }; sprintf_s(name, 32, "book_%d", i); Book *pBook = student1->add_arrbook(); pBook->set_name(name); pBook->set_price(1.2f * (i + 1)); pBook->set_pages((i + 1) * 15); } //printf("%s\n", student1->DebugString().c_str()); char buf[1024] = {0}; int32_t len = student1->ByteSize(); student1->SerializeToArray(buf, len); printf("btye size = %d\n", len); Student student2; student2.ParseFromArray(buf, len); printf("%s\n", student2.DebugString().c_str()); getchar(); return 0;}
注意事項:在屬性面板中把運行庫設置為 MT
編譯運行,成功,結果如下
附:編譯MinGW版本protobuf的腳本,與build_VS.bat大同小異
文件:build_MinGW.bat
內容:
::參考文章 https://github.com/google/protobuf/blob/master/cmake/README.md::默認當前操作系統(tǒng)已安裝 git 和 cmake 和 MinGW,并配置好了環(huán)境變量echo off & color 0A::設置所需要的Protobuf版本,最新版本可以在github上查到 https://github.com/google/protobuf::必須與下載的版本一致set PROTOBUF_VESION="3.0.0-beta-4"echo %PROTOBUF_VESION%set PROTOBUF_PATH="protobuf_%PROTOBUF_VESION%"echo %PROTOBUF_PATH%cd %PROTOBUF_PATH%::設置工程文件夾名字set BUILD_PATH="build_MinGW"::設置編譯版本 Debug Or Releaseset MODE="Release"cd cmakeif not exist %BUILD_PATH% md %BUILD_PATH%cd %BUILD_PATH%if not exist %MODE% md %MODE%cd %MODE%::開始構建編譯cmake ../../ -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=%MODE%mingw32-make.exeecho %cd%pause