Hossein
Hossein

Reputation: 26004

How to generate the respective protobuf files in Visual Studio during the compilation process automatically?

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?

Upvotes: 1

Views: 3493

Answers (1)

Jeaninez - MSFT
Jeaninez - MSFT

Reputation: 4040

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

Upvotes: 2

Related Questions