Generation of C# files with Google Protocol Fails

I am working on a project that uses Java, C# and also C++ applications. To communicate between them I am trying to use Google protocol buffer. I am using following .proto file, which was taken from examples:

package tutorial;

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}

message AddressBook {
  repeated Person person = 1;
}

I am referring to following tutorial: https://developers.google.com/protocol-buffers/docs/csharptutorial

Tutorials for other languages are also there.

I tried following command line arguments for each language:

Java :

C:ProtoBufprotoc -I=C:trash --java_out=C:trash C:trash/addressbook.proto

C++ :

C:ProtoBufprotoc -I=C:trash --cpp_out=C:trash C:trash/addressbook.proto

C# :

C:ProtoBufprotoc -I=C:trash --csharp_out=C:trash C:trash/addressbook.proto

Java and C++ compilations work properly even with some warning in case of Java. But I get following output with C# :

--csharp_out: protoc-gen-csharp: The system cannot find the file specified.

I am using this compiler: https://github.com/google/protobuf/releases/download/v2.6.1/protoc-2.6.1-win32.zip

What am I doing wrong here? do I need any other files for C# compilation?


I know how to gen proto files in c#

  • open visual studio, open nuget command line, type : Install-Package Google.ProtocolBuffers , link : Google.ProtocolBuffers 2.4.1.555
  • find Package/Google.ProtocolBuffers.2.4.1.555/tools/ProtoGen.exe
  • use command line, type : ProtoGen.exe addressbook.proto -output_directory=C:trash
  • I write a python script to gen proto files, gen.py

    import os, subprocess, threading
    
    def main():
        with open("conf.txt") as file:
            exe = os.path.join(os.getcwd(), "..PackageGoogle.ProtocolBuffers.2.4.1.555toolsProtoGen.exe")
            out = "-output_directory=%s" % (os.path.join(os.getcwd(), "..Commonlibsprotos"))
            def gen(proto):
                subprocess.check_call([exe, os.path.join("protos", proto), out])
            list = []
            for proto in file.read().split(','):
                t = threading.Thread(target = gen, args = (proto, ))
                t.start()
                list.append(t)
            for t in list:
                t.join()
    
    if __name__ == '__main__':
        main()
    

    conf.txt

    base.proto,test.proto,addressbook.proto
    

    You are trying to generate C# files using the old version of the protoc

    protoc-2.6.1-win32.zip

    C# code generator for both proto2 and proto3 was introduced only in Version 3.0.0-alpha-3

    Introduced two new language implementations (Objective-C, C#) to proto3.

    So, download protoc Version 3.0.0-alpha-3, install it and call: protoc -I=$SRC_DIR --csharp_out=$DST_DIR $SRC_DIR/your.proto

    Beware that started from Version 3.0.0-beta-1 C# code generator only supports generating proto3:

    Proto3 semantics supported; proto2 files are prohibited for C# codegen

    链接地址: http://www.djcxy.com/p/64792.html

    上一篇: Ubuntu上的Protobuf没有编译

    下一篇: 使用Google协议生成C#文件失败