One-click compilation of various versions of PROTOBUF on Windows

Source: Internet
Author: User

Tools Required: CMake for Windows and git for Windows

Principle: Protobuf is a Google Open source project, its source code on GitHub can be downloaded to, and the source is used CMake to build, so we can download the source to local, and then use CMake build Local project, and then compile.

Step one: Download the source code

Copy the following code, save it to the Download_protobuf_source.bat file, and run it

:: Reference article HTTPS://GITHUB.COM/GOOGLE/PROTOBUF/BLOB/MASTER/CMAKE/README.MD:: Default the current operating system has git and cmake installed, and the environment variable is configured echo off & Color 0 A:: Set the required PROTOBUF version, the latest version can be found on 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%:: Pull Protobuf source code from Githug git clone-b%protobuf_vesion% https://github.com/google/protobuf.git%protobuf_path%:: Pull gmockcd%protobuf_path%git clone-b release-1.7.0 https://github.com/google/googlemock.git gmock from GitHub:: Pull Gtestcd gmockgit clone-b release-1.7.0 https://github.com/google/googletest.git gtestpause from GitHub

Step two: Compile

You can use CMake to build the version you need, and here's an example of building and compiling a VS2013 version of Protobuf

  

Example: Building a VS2013 version

Copy the following code, save to the Build_vs.bat file, place it in the Download_protobuf_source.bat sibling directory, and then execute

For example

:: Reference article HTTPS://GITHUB.COM/GOOGLE/PROTOBUF/BLOB/MASTER/CMAKE/README.MD:: Default the current operating system has git and cmake installed, and the environment variable is configured echo off & Color 0 A:: Set the required PROTOBUF version, the latest version can be found on GitHub https://github.com/google/protobuf:: Must be consistent with the downloaded version of the set protobuf_ vesion= "3.0.0-beta-4" Echo%protobuf_vesion%set protobuf_path= "protobuf_%protobuf_vesion%" echo%PROTOBUF_PATH%cd% protobuf_path%:: Set the VS Toolset, equivalent to the specified VS version, depending on the installation path of VS set vs_dev_cmd= "D:\Program Files (x86) \microsoft Visual Studio 12.0\ Common7\tools\vsdevcmd.bat ":: Set the project folder name to distinguish different vs version set Build_path=" build_vs2013 ":: Set the compiled version of 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%:: Start building and compiling CALs L%vs_dev_cmd%cmake. /.. /g "NMake makefiles"-dcmake_build_type=%mode%call extract_includes.batnmake/f makefileecho%cd%pause

Of course, you can also compile your desired vs version by modifying the script above, detailed parameter comments

When the progress reaches 100%, the compilation is completed

At this point, everything has been generated, including the header files and Lib files

Test

New VS2013 project, set up the Google/protobuf header file directory and Lib library directory, linked to Libprotobuf.lib or libprotobuf-lite.lib, not mentioned here.

To create a new Protocol.proto file, enter the following protocol, note that it is important to include syntax = "Proto2" To specify the syntax rule version , otherwise a warning will be reported during the execution of Protoc.exe. If there is no effect, the default is The grammatical rules of Proto2

Specifies the syntax rule 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;}

New build Protocol Script Gen.bat, enter the following:

@echo off & Color 0 A:: Protoc program Name set "Protoc_exe=protoc.exe"::. proto file name set "Protoc_file_name=protocol.proto" set " protoc_path=%cd% "Set" cpp_out_path=%cd% ":: Generate. H and. CC"%protoc_path%\%protoc_exe% "--proto_path="%protoc_path% "--cpp _out= "%cpp_out_path%" "%protoc_path%\%protoc_file_name%" pause

Add the generated protocol.pb.h and protocol.pb.cc to the project you just made.

For example

Enter the code:

#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, +, "book_%d", I);        Book *pbook = Student1->add_arrbook ();        Pbook->set_name (name);        Pbook->set_price (1.2f * (i + 1));        Pbook->set_pages ((i + 1) *);    }    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;}

Caveats: Set the runtime to MT in the properties panel

Compile run, success, result as follows

Attached: Compiled MinGW version protobuf script, similar to Build_vs.bat

Files: Build_mingw.bat

Content:

:: Reference article HTTPS://GITHUB.COM/GOOGLE/PROTOBUF/BLOB/MASTER/CMAKE/README.MD:: Default the current operating system has git and CMake and MinGW installed, and the environment variable is configured echo Off & Color 0 A:: Set the required PROTOBUF version, the latest version can be found on GitHub https://github.com/google/protobuf:: Must be consistent with the downloaded version of the set protobuf_ vesion= "3.0.0-beta-4" Echo%protobuf_vesion%set protobuf_path= "protobuf_%protobuf_vesion%" echo%PROTOBUF_PATH%cd% protobuf_path%:: Set project folder name set Build_path= "Build_mingw":: Set compile version 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%:: Start Build compilation CMake. /.. /g "MinGW makefiles"-dcmake_build_type=%mode%mingw32-make.exeecho%cd%pause

One-click compilation of various versions of PROTOBUF on Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.