infoclogged
infoclogged

Reputation: 4017

Adding header files protobuf to CPP

I am using protobuf to generate cpp code.

In my imagereader.prototxt

node {
    name: "Image Reader";
    type: ImageReader;
        name: "path";
        type: pb_string;
        #value: "${DIR_DATA}/images/";
        value: "/local/git/data/images"";
    }
}

The code is generated depending on the content of the file. i.e in the generated cpp file I have a line

/* 
 * declare instance of node "Image Reader"
 */
sandbox::images::ImageReader node_0(std::string("/local/git/data/images/"));

The problem is that ofcourse if another developer uses the code, then it will fail because he/ she might not have the same directory structure. I tried with an environment variable DIR_DATA in the prototxt file, but the compiler generates the file with exactly the name DIR_DATA which ofcourse cannot be found.

/* 
 * declare instance of node "Image Reader"
 */
sandbox::images::ImageReader node_0(std::string("${DIR_DATA}/images/"));

Is there any way, that protobuf adds a header file using a command in the prototxt file? That way, I can manually #define DIR_DATA in that header file.

Upvotes: 1

Views: 764

Answers (1)

Samael
Samael

Reputation: 61

This post is old but for any one interested:

Protobuf does not support importing comment from proto to generated class.

The good approach is to add the copyright header with the generating script. In my case i have a gen_proto_files.sh that will executed the different step of generating protobuf class and checking version making sure everything is ok (with color), you then append you header to it. Here a litle non file psecific example:

#!/bin/bash
HEADER="/* Your proprietary header here */\n\n"
FILES=$(find ./generated -type f \( -name "*.h" -o -name "*.cpp" \))

for FILE in $FILES; do
  echo -e "$HEADER$(cat $FILE)" > $FILE
done

Upvotes: 0

Related Questions