c++ 将头文件protobuf添加到CPP

bvjxkvbb  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(94)

我使用protobuf来生成cpp代码。
在我的imagereader.prototxt中

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

字符串
代码是根据文件的内容生成的。即在生成的cpp文件中,我有一行

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


问题是,当然,如果另一个开发人员使用代码,那么它会失败,因为他/她可能没有相同的目录结构。我尝试在prototxt文件中使用一个环境变量RISK_DATA,但编译器生成的文件与名称RISK_DATA完全相同,当然找不到。

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


有没有什么方法,protobuf可以在prototxt文件中添加一个头文件?这样,我就可以在那个头文件中手动#define_DATA。

bwitn5fc

bwitn5fc1#

这篇文章很旧,但对于任何感兴趣的人来说:
Protobuf不支持将注解从proto导入到生成的类。
好的方法是在生成脚本中添加版权头。在我的例子中,我有一个gen_proto_files.sh,它将执行生成protobuf类的不同步骤,并检查版本,确保一切正常(用颜色),然后将你的头附加到它。这里有一个小的非文件psecific示例:

#!/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

字符串

相关问题