Google Protocol Buffers Quick Start (with the method to generate C # source)

Source: Internet
Author: User

Google Protocol buffers is Google's production of a protocol generation tool, characterized by cross-platform, efficient, fast, to our own program definition and use of private protocol is very helpful.

Introduction to Protocol Buffers:

1, to http://code.google.com/p/protobuf/downloads/list download a source code package and a compiled binary package

2, find a proto sample code, use the command

protoc-i= $SRC _dir--java_out= $DST _dir $SRC _dir/addressbook.proto

Compile the proto file to get the corresponding Java source code. such as Protoc-i=src--java_out=addressbook.proto.

Java project configuration:

1. Use first

$ Protoc--java_out=src/main/java-i. /src.. /src/google/protobuf/descriptor.proto

The command compiles the Descriptor.proto in the source code package and generates a class.

2, the first step generated by the file with the source code in the same Java/src/main/java code package, thrown into your project engineering, this is the basic class library.

3, then you can write your own proto file, and then compile, together with the underlying class library thrown into your project project, you can run.

C # 's configuration:

1, Google itself does not generate C # source code function, to use C #, must use a CSharp port, the project address is: http://code.google.com/p/protobuf-csharp-port/

2, the source can be from svn above download down, download down, need to download a Nant, and then in the project source directory under the command operation, used to compile the build executable file

Nant Clean Build Test dist

This step is done by generating several files:

Protogen.exe-the Source code Generator

Google.protocolbuffers.dll-the Library part of the project

Protomunge.exe-tool to remove sensitive data from binary messages

Protodump.exe-tool to dump a binary message as text

Protobench.exe-tool to run performance benchmarks on binary messages

Mainly the first and second

The first ProtoGen.exe is used to generate C # source code.

The second Google.ProtocolBuffers.dll is used to reference in the project.

3, in the source code with a proto file, is based on the C # version specifically modified, you can take out to do the build test, execute the following command can get C # source files:

Protoc--descriptor_set_out=addressbook.protobin--proto_path=protos--include_imports protos\tutorial\ Addressbook.proto

Protogen Addressbook.protobin

If you want to generate C # source code, you need to do the first step to compile the source file with Protoc, compile and then use Protogen to generate a C # file, will generate three files, only AddressBookProtos.cs useful, throw this file together with Google.ProtocolBuffers.dll into the project and you can use it.

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.