Google Protocol Buffers Quick Start (with the method of generating C # source code)

Source: Internet
Author: User

Google Protocol buffers is a protocol generation tool produced by Google, which is characterized by its cross-platform, high efficiency and speed, and is helpful for our own program definition and use of proprietary protocols.

Protocol Buffers Introduction:
1, go 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. For example Protoc-i=src--java_out=addressbook.proto.

Java project configuration:
1. First Use
$ Protoc--java_out=src/main/java-i. /src. /src/google/protobuf/descriptor.proto
command to compile a descriptor.proto in the source code package to generate a class.
2, the first step to generate the file even in the same source code package in the Java/src/main/java, and dropped into your project projects, this is the base Class library.
3, then you can write your own proto file, and then compile, along with the above basic class library dropped into your project project, you can run.

C # Configuration:
1, Google itself does not bring the ability to generate C # source code, to use C #, you must use a CSharp port, the project address is: http://code.google.com/p/protobuf-csharp-port/
2, the source code can be download down from svn, download down, you need to download a Nant, and then in the project source directory for command operation, to compile and build the executable file

Nant Clean Build Test dist

This step is done to generate 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


Primarily the first and second
The first ProtoGen.exe is the one used to generate C # source code
The second Google.ProtocolBuffers.dll is used to reference in a project.

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

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 a 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 is useful, throw this file together with Google.ProtocolBuffers.dll and you will be able to use it.

---------------------------------------------------------

Google Protocol Buffers Quick Start (with the method of generating C # source code)

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.