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、运行可执行程序