1

I have several protobuf messages in a folder which I'd like to automatically convert into the respective header/cc files and then continue the compilation process inside of Visual Studio.
The best solution that I could comeup sofar was to define a Pre-Build Event through Propertise>Build Events>Pre-Build Event and specifying the following as the command:

$(SolutionDir)Dependencies\include\protobuf\bin\protoc.exe --proto_path=$(SolutionDir)Dependencies\include\messages\ --cpp_out=$(SolutionDir)Dependencies\include\messages\ message.proto message2.proto message3.proto

There are currently 2 issues concerning this solution :

  1. I have to manually add each filename myself. How is it possible to make the filenames get picked automatically by VS2019? I tried %filename% macro, to no avail since it seems it returns the project file names only.

  2. I also found out, these files are not generated each time I change the messages. even cleaning the projects, doesn't delete them, so I have to manually delete the generated files and try rebuilding the project again!

Other than resorting to a batchfile that can get called as a prebuild event, how can I achieve this inside Visual Studio without doing that?

vvv444
  • 2,764
  • 1
  • 14
  • 25
Hossein
  • 24,202
  • 35
  • 119
  • 224

1 Answers1

2

I suggest you could refer to the following steps:

  1. Modify the properties of the .proto file: Item Type: Custom Build Tool

  2. Configure project properties: Properties -> Custom Build Tools -> General

    Command line: $(SolutionDir)Dependencies\include\protobuf\bin\protoc.exe --proto_path= .\proto %(Filename).proto --cpp_out=$(ProjectDir)protocpp

    Description: protoc %(Filename).proto

    Outputs: $(ProjectDir)protocpp%(Filename).pb.cc

    Add Outputs to Item Type: C/C++ complier

And then you could try to build the .proto file.

Note: The newly added the .proto file also needs to select the operation of the Custom Build Tool

vvv444
  • 2,764
  • 1
  • 14
  • 25
Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20
  • Thanks a lot really appreciate it. I'm getting `cmd exited with code 1` error. I only changed the output directory that is : ```$(SolutionDir)Dependencies\include\protobuf\bin\protoc.exe --proto_path= .\proto %(Filename).proto --cpp_out=$(SolutionDir)Dependencies\include\messages\``` whats wrong here? – Hossein Aug 12 '20 at 02:57
  • also is it possible to get the output of protoc execution in visual studio? for example, we it fails, the protoc actually prints the error (where the problem is found) but currently in VS, it just says the build failed! and I need to rebuild the protoc outside of vs in a cmd to see the actual error. – Hossein Aug 12 '20 at 03:00
  • @Rika,As far as I'm concerned you should make sure the path behind `--cpp_out=` in the command line is consistent with the path in `Outputs`. – Jeaninez - MSFT Aug 12 '20 at 05:14