使用protobuf的简单流程记录、编译protobuf时遇到的坑 以及 链接protobuf的坑

简介: 使用protobuf的简单流程记录、编译protobuf时遇到的坑 以及 链接protobuf的坑

protobuf 简单流程:

1、写.proto文件,语法 可以看

https://blog.csdn.net/lcuwb/article/details/90705397

2、使用指令编译proto文件,如果是生成c++ 的资源,则会生成 cc文件, 和 .h文件

protoc -I[proto文件的路径]  --cpp_out=./[cc, .h文件生成的路径]  *.proto(proto文件的位置)

protoc -I./ -cpp_out=./ ./project.proto

3、写一个cpp文件,应用protobuf

      include  proto 生成的.h文件

      对数据进行序列化,和反序列化,进行调试

4、编译的时候 需要加上 -std=c++1 , -lthread , -lprotobuf           ---最坑的位置在这里

例如:

g++ test_proto.cpp project.pb.cc -o test_proto -lprotobuf -std=c++11 -lpthread

如果没有加上-lthread  会报以下错误

[libprotobuf FATAL google/protobuf/generated_message_util.cc:783] CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning):

 

如果没有加上-std=c++11 会报以下错误

/usr/local/include/google/protobuf/metadata_lite.h:160:52: error: no matching function for call to ‘google::protobuf::Arena::Create(google::protobuf::Arena*&)’
    Container* container = Arena::Create<Container>(my_arena);

 

如果没有加上-lprotobuf 会报以下错误

project.pb.cc:(.text+0x5fc): undefined reference to `google::protobuf::io::CodedInputStream::ReadTagFallback(unsigned int)'
project.pb.cc:(.text+0x723): undefined reference to `google::protobuf::internal::WireFormatLite::VerifyUtf8String(char const*, int, google::protobuf::internal::WireFormatLite::Operation, char const*)'
project.pb.cc:(.text+0x7b5): undefined reference to `google::protobuf::internal::WireFormatLite::VerifyUtf8String(char const*, int, google::protobuf::internal::WireFormatLite::Operation, char const*)'
project.pb.cc:(.text+0x849): undefined reference to `google::protobuf::internal::WireFormat::SkipField(google::protobuf::io::CodedInputStream*, unsigned int, google::protobuf::UnknownFieldSet*)'
/tmp/cc1U6w5J.o: In function `Account::SerializeWithCachedSizes(google::protobuf::io::CodedOutputStream*) const':
project.pb.cc:(.text+0x8eb): undefined reference to `google::protobuf::internal::WireFormatLite::WriteUInt64(int, unsigned long, google::protobuf::io::CodedOutputStream*)

 

5、运行可执行程序

 

 

 

相关文章
|
7月前
|
编解码 Java 编译器
【Protobuf】Protobuf中的Message语法规范
在Message中定义一个或者多个字段,FieldType是字段的数据类型,可以是基本类型(如int32、string、bool等)或其他定义的Message类型。fieldName是字段的名称,可以根据需求自定义。fieldNumber是字段的唯一标识号,用于在消息的二进制编码中标识字段。
132 0
|
4月前
|
Java 编译器 Go
Protobuf3语法笔记
Protobuf3语法笔记
57 0
|
6月前
|
JSON Linux 测试技术
go语言处理数据、基本通信以及环境配置 -- json,protobuf,grpc
go语言处理数据、基本通信以及环境配置 -- json,protobuf,grpc
|
9月前
|
XML 存储 JSON
数据序列化工具 Protobuf 编码&避坑指南
我们现在所有的协议、配置、数据库的表达都是以 protobuf 来进行承载的,所以我想深入总结一下 protobuf 这个协议,以免踩坑。 先简单介绍一下 Protocol Buffers(protobuf),它是 Google 开发的一种数据序列化协议(与 XML、JSON 类似)。它具有很多优点,但也有一些需要注意的缺点: 优点: 效率高:Protobuf 以二进制格式存储数据,比如 XML 和 JSON 等文本格式更紧凑,也更快。序列化和反序列化的速度也很快。 跨语言支持:Protobuf 支持多种编程语言,包括 C++、Java、Python 等。 清晰的结构定义:使用 prot
|
10月前
|
编译器
ProtoBuf的安装
ProtoBuf的安装
|
存储 JSON 缓存
Google序列化库FlatBuffers 1.1发布,及与protobuf的比较
Google序列化库FlatBuffers 1.1发布,及与protobuf的比较
2803 0
|
XML 数据格式 C++
protobuf C++ 使用示例
1、在.proto文件中定义消息格式 2、使用protobuf编译器 3、使用c++ api来读写消息   0、为何使用protobuf?   1、原始内存数据结构,可以以二进制方式sent/saved.这种方式需要相同的内存布局和字节序。
8098 0
|
C++ Python Java
protobuf 更新消息和扩展,包
一、更新一个消息类型 如果一个已有的消息格式已无法满足新的需求——如,要在消息中添加一个额外的字段——但是同时旧版本写的代码仍然可用。不用担心!更新消息而不破坏已有代码是非常简单的。
1172 0
|
Java Android开发
【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 使用文档 | 创建 Protobuf 源文件 | Protobuf 语法 )
【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 使用文档 | 创建 Protobuf 源文件 | Protobuf 语法 )
292 0
【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 使用文档 | 创建 Protobuf 源文件 | Protobuf 语法 )