在Windows上一鍵編譯各種版本的Protobuf

來源:互聯網
上載者:User

標籤:str   getchar   ring   for   3.0   分享   windows   git   option   

所需工具 : 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::預設當前作業系統已安裝 git 和 cmake,並配置好了環境變數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 同級目錄,然後執行

  例如

 

::參考文章 https://github.com/google/protobuf/blob/master/cmake/README.md::預設當前作業系統已安裝 git 和 cmake,並配置好了環境變數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"::設定工程檔案夾名字,用來區分不同的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版本,具體的參數注釋的很詳細

當進度達到100%的時候,說明編譯完成

此時,所有的東西都已經產生,包括標頭檔 和 lib檔案

測試

  建立VS2013工程,設定好google/protobuf 標頭檔目錄 和 lib庫目錄,連結到 libprotobuf.lib 或者 libprotobuf-lite.lib ,此處不贅述.

  建立 protocol.proto 檔案,輸入以下協議,需要注意的是,一定要加入 syntax = "proto2" 指定文法規則版本,否則在執行 protoc.exe 的過程中會警示告.如果不加也沒有影響,預設為 proto2 的文法規則

// 指定文法規則 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;}

  建立產生協議指令碼 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::預設當前作業系統已安裝 git 和 cmake 和 MinGW,並配置好了環境變數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

在Windows上一鍵編譯各種版本的Protobuf

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.