6
votes

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:\ProtoBuf\protoc -I=C:\trash --java_out=C:\trash C:\trash/addressbook.proto

C++:

C:\ProtoBuf\protoc -I=C:\trash --cpp_out=C:\trash C:\trash/addressbook.proto

C#:

C:\ProtoBuf\protoc -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?

3

3 Answers

4
votes

I know how to gen proto files in c#

  1. open visual studio, open nuget command line, type : Install-Package Google.ProtocolBuffers , link : Google.ProtocolBuffers 2.4.1.555
  2. find Package/Google.ProtocolBuffers.2.4.1.555/tools/ProtoGen.exe
  3. 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(), "..\\Package\\Google.ProtocolBuffers.2.4.1.555\\tools\\ProtoGen.exe")
        out = "-output_directory=%s" % (os.path.join(os.getcwd(), "..\\Common\\libs\\protos"))
        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
6
votes

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

0
votes

I solved all my problems using / (slash) intead of \ (anti-slash) in all the path.

protoc.exe -I=D:/dev/xyz/projects --csharp_out=d:/temp D:/dev/xyz/projects/messages.proto