protobuf descriptor

  1. cpp中如何使用pb.h :protobuf入门教程(六):导入定义(import)
  2. protoc编译找不到library或者 linker failed, 直接binary重新编一把 https://blog.csdn.net/Programmer_H/article/details/8890800

test.cpp

#include 
#include 

#include 
#include 
using namespace std;
class MyErrorCollector
   : public google::protobuf::compiler::MultiFileErrorCollector {
 virtual void AddError(const std::string& filename, int line, int column,
                       const std::string& message) {
   // define import error collector
   printf("%s, %d, %d, %s\n", filename.c_str(), line, column, message.c_str());
 }
};

int main() {
 google::protobuf::compiler::DiskSourceTree sourceTree;  // source tree
 sourceTree.MapPath("", "./");
 MyErrorCollector errorCollector;
 google::protobuf::compiler::Importer importer(&sourceTree, &errorCollector);
 const google::protobuf::FileDescriptor* filedes =
     importer.Import("test.proto");
// cout<< "file des = "<DebugString()<DebugString();

 // create a dynamic message factory
 google::protobuf::DynamicMessageFactory* factory =
     new google::protobuf::DynamicMessageFactory(importer.pool());
 // create a const message ptr by factory method and message descriptor
 const google::protobuf::Message* tmp = factory->GetPrototype(descriptor);

 // create a non-const message object by const message ptr
 // define import error collector
 google::protobuf::Message* msg = tmp->New();

 return 0;
}

test.proto


// test.proto

syntax = "proto3";

package lm;

message helloworld

{

  int32 id = 1;

  string name = 2;

  repeated string hero = 3;
}

编译命令clang++ test.cpppkg-config --libs --cflags protobuf`

目录结构:
在这里插入图片描述

`

你可能感兴趣的:(Linux学习)