CompuPlanet
CompuPlanet

Reputation: 151

convert c++ header file to protobuf .proto file

I am wondering, is there any tool that I can use to convert a .h file into a .proto file for c++? What I want to do is: I have several structs into a .h file, and I want to serialize them and send them via protbuf. To do that, I need a .proto file to generate the .pb.h and .pb.cc files and then I will be able serialize the structs and send them.

Does anybody know if there is such a tool? I could do it manually but I will take much time because the header file contains many structs.

Thank you!

I am editing my question to let you know that I haven't found what I was asking for, but I found this https://code.google.com/p/protobuf-csharp-port/wiki/ProtoGen tool which was also very helpful. You give as parameter the .proto file and it generates the c# code. Hope it helps!

Upvotes: 11

Views: 4226

Answers (2)

laog
laog

Reputation: 51

Following code maybe help:

for line in open("frame.v2.pb.h"):
    if (line.startswith("class") and line.find("::google::protobuf::Message") > 0 ) \
            or (line.startswith("  //") and line.find("@@protoc_insertion_point") < 0 and
                    line.find("--------")<0 ) \
            or line.startswith("namespace ") \
            :
        print(line.strip())

Upvotes: 0

malik
malik

Reputation: 66

You can write a script that will do a good job using libclang Python bindings.

First, generate a compile_commands.json using CMake for your project. Parse the complete compiler command line for your code of interest.

Second, pass the compile command line to libclang from your Python script to parse a compilation unit, that contains your header of interest.

Third, iterate the AST and extract your structs of interest.

Fourth, generate protobuf from the isolated AST sub-trees.

This is just a sketch, but I did something similar to recursively find all headers included in a compilation unit.

Upvotes: 1

Related Questions