简体   繁体   中英

Generating c# files from .proto files using protoc on pre build event in VS 2017

I tried to generate c# files from.proto files using protoc from google.protobuf.tools nuget package on pre build event in Visual Studio 2017 for all.proto files in particular folder. I created .net core library project with.proto files.

Pre-build event command line for for that

protoc -I=$(ProjectDir)Messages --csharp_out=$(ProjectDir)Messages $(ProjectDir)*.proto

where protoc is environment variable for precompiled version of protoc.exe

But I got error

 error MSB3073: The command "protoc -I=C:\Projects\TecAlliance.Core.Messages\TecAlliance.Core.Messages\Messages --csharp_out=C:\Projects\TecAlliance.Core.Messages\TecAlliance.Core.Messages\Messages C:\Projects\TecAlliance.Core.Messages\TecAlliance.Core.Messages\GpsCoodinates.proto" exited with code 9009.
1>Done building project "TecAlliance.Core.Messages.csproj" -- FAILED.

How can I resolve it?

These links were helpful:

NugetPackageRoot

MSBuild Prebuild

The Pre-build command I got to work:

$(NugetPackageRoot)google.protobuf.tools\3.13.0\tools\windows_x64\protoc.exe -I=$(ProjectDir)protos --csharp_out=$(ProjectDir)Messages $(ProjectDir)protos\*.proto

I did not manually install protoc. This command is using the one that came with the Nuget package google.protobuf.tools.

My project structure is as follows:

-Project directory

----Messages directory

------myMessages.cs

----protos directory

------myMessages.proto

To answer the specific question:

I received the same error and it means that it can't find "protoc". By using a build command like above, then it will be able to find the protoc exe. An obvious downside is that if you update the protobuf packages then you will also need to update the version in MSBuild command. Maybe someone knows a way to automate that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM