Protocol_buff using the Mingw32 compilation method under Windows

Source: Internet
Author: User

Recently took over a project in the message using the Protocol_buff (hereinafter referred to as PROTOBUF), is a very convenient message structure, has not been contacted before, then recorded in the Windows learning process

Introduction of Protobuf (from Baidu Encyclopedia)

Protobuf (PB) is a format for Google's data interchange, which is independent of the language and is platform independent. Google offers implementations in multiple languages: Java, C #, C + +, go and Python, each of which contains compilers and library files for the appropriate language. Because it is a binary format, it is much faster than using XML for data exchange. It can be used in data communication between distributed applications or in heterogeneous environments. As a good efficiency and compatibility of the binary data transmission format, can be used for such as network transport, configuration files, data storage and many other areas.

Download of Protobuf


Protobuf Source code Download path: http://code.google.com/p/protobuf/downloads/list (no FQ can you open it?) Anyway, I can't turn it off.
GitHub Download Path: https://github.com/google/protobuf

Accidentally under more, the Trunk branch and release all down, the current version is 3.4.0

Compiling protobuf

Know Protobuf's classmates all know protobuf before using to build Protoc.exe, and then compile the Protoc.exe we wrote. Proto type of file.

Compilation environment: Win7 X64
Compiler: MINGW32
cmake:3.9.2
First into the Protobuf file, here we use the PROTOBUF version is 3.4.0, compiled in the tags/3.4.0/folder
Open Cmake-gui,
The where is the source code check box is populated with the CMake folder in version 3.4 (cmake file used by Cmakelists.txt in this folder)
In the Where to build the binaries check box, fill in the Build folder under the CMake folder (this can build a build folder yourself)
In the system environment variable, add the Bin folder in the CMake installation directory and the Bin folder under the MinGW installation directory. For example my current machine: D:\programfiles\cmake\bin; C:\MinGW\bin
Make sure your mingw contains Mingw32-make.exe, and if not, execute the following command in CMD:
Mingw-get Install gcc g++ mingw32-make


Click Configure, configure select default MinGW makefile Balabala what ... , wait for CMake Auto Configuration, configure complete diagram:

Click Generate, generate makefile and other files


After generating the good makefile, download Gmock and gtest on GitHub, the following files are intercepted from the Readme file under the CMake folder (can also be skipped here, add parameter CMake in Protobuf_build_tests=off)

After download, create the Gmock folder in the Protobuf folder, copy the code in the Gmock into it, and then create a gtest folder in the Gmock folder to copy the files from the Gtest folder to it, such as:

Open cmd, switch to the current directory, execute the mingw32-make command and wait for the make command to complete

My side is running to 73% ... Check tests dependencies will be error ... Haven't figured out why it's going to be an error, but tests looks like we don't? (I think so) read the file, we need the Protoc.exe has been generated, just ignore it. (later carefully looked at the next makefile, the original can be added in the CMake parameter-dprotobuf_build_tests=off to close the test, you do not need to download Gmock and gtest again)

Protoc.exe has been generated.

Compilation of. Proto files


Put the generated protoc.exe file into the well-written. proto file path, and open the command line to switch to that directory
Execute command: PROTOC.EXE-PROTO_PATH=SRC--cpp_out=dst Src/test.proto
Parameters: Src:proto file folder DST: Generate destination folder Src/test.proto source file name

The generated *.pb.h,*.pb.cc file is placed under the project folder, the project header file reference increases the PROTOBUF/SRC path, the link good add the LIBPROTOBUF.A generated in the build folder can be compiled

Attach a test case:


#include <iostream>
#include "Test.pb.h"
using namespace Std;

int main ()
{
Google_protobuf_verify_version;
string send;
Pblxtest::p blxtest test1;
Pblxtest::p blxtest test2;
Test1.set_name ("testname");
Test1.set_age (23);
cout << test1. IsInitialized () << Endl;
Test1. Serializetostring (&send);
cout << send << Endl;
Test2. Parsefromstring (send);

cout << test2.name () << "<< test2.age () << Endl;
return 0;
}

Protocol_buff using the Mingw32 compilation method under Windows

Related Article

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.