C++ protobuf测试使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yang332233/article/details/83657735

我电脑ubuntu16.04,我们工程里面用的protobuf-3.1.0版本的,是指定目录安装的,我打开其安装目录,

 

所以不管是这个,其他的比如opencv也是这样子的,编译生成这三种

在QT工程里面的pro这么写的:

OPENCV_ROOT_PATH = /home/software
CAFFE_ROOT_PATH = /media/d/caffe_root
PROTOBUF_ROOT_PATH = /home/software/protobuf-3.1.0

INCLUDEPATH += $${OPENCV_ROOT_PATH}/include \
               $${OPENCV_ROOT_PATH}/include/opencv \
               $${OPENCV_ROOT_PATH}/include/opencv2 \
               $${CAFFE_ROOT_PATH}/include \
               $${CAFFE_ROOT_PATH}/build/include \
               $${PROTOBUF_ROOT_PATH}/include \
               /usr/local/cuda-8.0/include

LIBS += -L$${OPENCV_ROOT_PATH}/lib \
        -L$${CAFFE_ROOT_PATH}/build/lib \
        -L$${PROTOBUF_ROOT_PATH}/lib \
        -L/usr/local/cuda-8.0/lib64

LIBS += -lopencv_core \
        -lopencv_highgui \
        -lopencv_imgproc \
        -lopencv_imgcodecs \
        -lopencv_videoio \
        -lcaffe

我不知道这里为什么没有加LIBS += -lprotobuf的

然后我按照这个博客测试protobuf的用法https://blog.csdn.net/u014538198/article/details/72674742

需要用系统自带的proto,我搜索我电脑proto放在哪里,/usr/bin/protoc 是在这里,然后敲命令:

/usr/bin$ ./protoc -I=/media/d_2/everyday/1102/proto_test/src --cpp_out=/media/d_2/everyday/1102/proto_test/out /media/d_2/everyday/1102/proto_test/src/person.proto 

果真生成了两个文件

/media/d_2/everyday/1102/proto_test/out/person.pb.cc
/media/d_2/everyday/1102/proto_test/out/person.pb.h

然后我各种locate 查找库文件和头文件在哪里,库文件在/usr/lib/x86_64-linux-gnu,头文件不太好找,locate google才翻到的,经过对比比较测试,pro这么写才正确:

QT += core
QT -= gui

CONFIG += c++11

TARGET = 1102test_proto
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app


INCLUDEPATH += /usr/include

LIBS += -L/usr/lib/x86_64-linux-gnu

LIBS += -lprotobuf



SOURCES += main.cpp \
    person.pb.cc

DEFINES += QT_DEPRECATED_WARNINGS


HEADERS += \
    person.pb.h
#include "person.pb.h"
#include<iostream>
#include<fstream>

int main(int argc, char *argv[])
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    Test::Person person;
    person.set_id( 123 );
    person.set_name( "abc" );
    person.set_email( "[email protected]" );

    std::fstream out( "/media/d_2/everyday/1102/person.proto", std::ios::out | std::ios::binary | std::ios::trunc );
    person.SerializeToOstream( &out );
    out.close();

    // 从文件中读取数据, 并且反序列化
   Test::Person person1;
   std::fstream in( "/media/d_2/everyday/1102/person.proto", std::ios::in | std::ios::binary );
   if ( !person1.ParseFromIstream( &in ) ) {
     std::cerr << "Failed to parse person.pb." << std::endl;
     exit(1);
   }

   std::cout << "ID: " << person1.id() << std::endl;
   std::cout << "name: " << person1.name() << std::endl;
   if ( person1.has_email() ) {
     std::cout << "e-mail: " << person1.email() << std::endl;
   }

   // Optional:  Delete all global objects allocated by libprotobuf.
   google::protobuf::ShutdownProtobufLibrary();

   getchar();

return 0;




    return 1;
}

一开始LIBS += -lprotobuf这个没有写,编译可以通过,但是运行各种报错,什么未定义的引用,如下,所以以后出现什么未定义的引用,就是库的原因,有一个问题是工程里面没有加LIBS += -lprotobuf可以没问题,不知道原因...

17:01:43: 为项目1102test_proto执行步骤 ...
17:01:43: 配置没有改变, 跳过 qmake 步骤。
17:01:43: 正在启动 "/usr/bin/make" 

g++ -Wl,-rpath,/home/yhl/Qt/5.9.1/gcc_64/lib -o 1102test_proto main.o person.pb.o   -L/usr/lib/x86_64-linux-gnu -L/home/yhl/Qt/5.9.1/gcc_64/lib -lQt5Core -lpthread 
main.o:在函数‘main’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/main.cpp:7:对‘google::protobuf::internal::VerifyVersion(int, int, char const*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/main.cpp:14:对‘google::protobuf::Message::SerializeToOstream(std::ostream*) const’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/main.cpp:20:对‘google::protobuf::Message::ParseFromIstream(std::istream*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/main.cpp:32:对‘google::protobuf::ShutdownProtobufLibrary()’未定义的引用
main.o:在函数‘google::protobuf::internal::GetEmptyStringAlreadyInited[abi:cxx11]()’中:
/usr/include/google/protobuf/generated_message_util.h:80:对‘google::protobuf::internal::empty_string_[abi:cxx11]’未定义的引用
/usr/include/google/protobuf/generated_message_util.h:81:对‘google::protobuf::internal::empty_string_[abi:cxx11]’未定义的引用
person.pb.o:在函数‘Test::protobuf_AssignDesc_person_2eproto()’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:33:对‘google::protobuf::DescriptorPool::generated_pool()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:34:对‘google::protobuf::DescriptorPool::FindFileByName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:35:对‘google::protobuf::internal::LogMessage::LogMessage(google::protobuf::LogLevel, char const*, int)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:35:对‘google::protobuf::internal::LogMessage::operator<<(char const*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:35:对‘google::protobuf::internal::LogFinisher::operator=(google::protobuf::internal::LogMessage&)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:35:对‘google::protobuf::internal::LogMessage::~LogMessage()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:52:对‘google::protobuf::DescriptorPool::generated_pool()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:52:对‘google::protobuf::MessageFactory::generated_factory()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:52:对‘google::protobuf::internal::GeneratedMessageReflection::GeneratedMessageReflection(google::protobuf::Descriptor const*, google::protobuf::Message const*, int const*, int, int, int, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, int)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:35:对‘google::protobuf::internal::LogMessage::~LogMessage()’未定义的引用
person.pb.o:在函数‘Test::(anonymous namespace)::protobuf_RegisterTypes(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:66:对‘google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(google::protobuf::Descriptor const*, google::protobuf::Message const*)’未定义的引用
person.pb.o:在函数‘Test::protobuf_AddDesc_person_2eproto()’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:80:对‘google::protobuf::internal::VerifyVersion(int, int, char const*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:84:对‘google::protobuf::DescriptorPool::InternalAddGeneratedFile(void const*, int)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:86:对‘google::protobuf::MessageFactory::InternalRegisterGeneratedFile(char const*, void (*)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&))’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:89:对‘google::protobuf::internal::OnShutdown(void (*)())’未定义的引用
person.pb.o:在函数‘Test::Person::Person()’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:108:对‘google::protobuf::UnknownFieldSet::UnknownFieldSet()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:108:对‘google::protobuf::UnknownFieldSet::~UnknownFieldSet()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:108:对‘google::protobuf::Message::~Message()’未定义的引用
person.pb.o:在函数‘Test::Person::Person(Test::Person const&)’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:117:对‘google::protobuf::UnknownFieldSet::UnknownFieldSet()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:117:对‘google::protobuf::UnknownFieldSet::~UnknownFieldSet()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:117:对‘google::protobuf::Message::~Message()’未定义的引用
person.pb.o:在函数‘Test::Person::~Person()’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:132:对‘google::protobuf::UnknownFieldSet::~UnknownFieldSet()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:132:对‘google::protobuf::Message::~Message()’未定义的引用
person.pb.o:在函数‘google::protobuf::io::CodedInputStream::ReadTagWithCutoff(unsigned int)’中:
/usr/include/google/protobuf/io/coded_stream.h:924:对‘google::protobuf::io::CodedInputStream::ReadTagFallback()’未定义的引用
person.pb.o:在函数‘Test::Person::MergePartialFromCodedStream(google::protobuf::io::CodedInputStream*)’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:200:对‘google::protobuf::internal::WireFormatLite::ReadString(google::protobuf::io::CodedInputStream*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:232:对‘google::protobuf::internal::WireFormatLite::ReadString(google::protobuf::io::CodedInputStream*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:252:对‘google::protobuf::internal::WireFormat::SkipField(google::protobuf::io::CodedInputStream*, unsigned int, google::protobuf::UnknownFieldSet*)’未定义的引用
person.pb.o:在函数‘Test::Person::SerializeWithCachedSizes(google::protobuf::io::CodedOutputStream*) const’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:277:对‘google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::io::CodedOutputStream*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:282:对‘google::protobuf::internal::WireFormatLite::WriteInt32(int, int, google::protobuf::io::CodedOutputStream*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:292:对‘google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::io::CodedOutputStream*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:297:对‘google::protobuf::internal::WireFormat::SerializeUnknownFields(google::protobuf::UnknownFieldSet const&, google::protobuf::io::CodedOutputStream*)’未定义的引用
person.pb.o:在函数‘google::protobuf::io::CodedOutputStream::WriteTagToArray(unsigned int, unsigned char*)’中:
/usr/include/google/protobuf/io/coded_stream.h:1083:对‘google::protobuf::io::CodedOutputStream::WriteVarint32FallbackToArray(unsigned int, unsigned char*)’未定义的引用
person.pb.o:在函数‘google::protobuf::internal::WireFormatLite::WriteStringToArray(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)’中:
/usr/include/google/protobuf/wire_format_lite_inl.h:749:对‘google::protobuf::io::CodedOutputStream::WriteStringWithSizeToArray(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)’未定义的引用
person.pb.o:在函数‘google::protobuf::io::CodedOutputStream::WriteTagToArray(unsigned int, unsigned char*)’中:
/usr/include/google/protobuf/io/coded_stream.h:1083:对‘google::protobuf::io::CodedOutputStream::WriteVarint32FallbackToArray(unsigned int, unsigned char*)’未定义的引用
/usr/include/google/protobuf/io/coded_stream.h:1083:对‘google::protobuf::io::CodedOutputStream::WriteVarint32FallbackToArray(unsigned int, unsigned char*)’未定义的引用
person.pb.o:在函数‘google::protobuf::internal::WireFormatLite::WriteStringToArray(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)’中:
/usr/include/google/protobuf/wire_format_lite_inl.h:749:对‘google::protobuf::io::CodedOutputStream::WriteStringWithSizeToArray(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)’未定义的引用
person.pb.o:在函数‘Test::Person::SerializeWithCachedSizesToArray(unsigned char*) const’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:334:对‘google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(google::protobuf::UnknownFieldSet const&, unsigned char*)’未定义的引用
person.pb.o:在函数‘Test::Person::ByteSize() const’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:369:对‘google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(google::protobuf::UnknownFieldSet const&)’未定义的引用
person.pb.o:在函数‘Test::Person::MergeFrom(google::protobuf::Message const&)’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:378:对‘google::protobuf::internal::LogMessage::LogMessage(google::protobuf::LogLevel, char const*, int)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:378:对‘google::protobuf::internal::LogMessage::operator<<(char const*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:378:对‘google::protobuf::internal::LogFinisher::operator=(google::protobuf::internal::LogMessage&)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:378:对‘google::protobuf::internal::LogMessage::~LogMessage()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:383:对‘google::protobuf::internal::ReflectionOps::Merge(google::protobuf::Message const&, google::protobuf::Message*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:378:对‘google::protobuf::internal::LogMessage::~LogMessage()’未定义的引用
person.pb.o:在函数‘Test::Person::MergeFrom(Test::Person const&)’中:
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:390:对‘google::protobuf::internal::LogMessage::LogMessage(google::protobuf::LogLevel, char const*, int)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:390:对‘google::protobuf::internal::LogMessage::operator<<(char const*)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:390:对‘google::protobuf::internal::LogFinisher::operator=(google::protobuf::internal::LogMessage&)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:390:对‘google::protobuf::internal::LogMessage::~LogMessage()’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:402:对‘google::protobuf::UnknownFieldSet::MergeFrom(google::protobuf::UnknownFieldSet const&)’未定义的引用
/media/d/Yang/Myproject/build-1102test_proto-Desktop_Qt_5_9_1_GCC_64bit-Debug/../1102test_proto/person.pb.cc:390:对‘google::protobuf::internal::LogMessage::~LogMessage()’未定义的引用
person.pb.o:在函数‘google::protobuf::Closure::Closure()’中:
/usr/include/google/protobuf/stubs/common.h:855:对‘vtable for google::protobuf::Closure’未定义的引用
person.pb.o:在函数‘google::protobuf::internal::FunctionClosure0::FunctionClosure0(void (*)(), bool)’中:
/usr/include/google/protobuf/stubs/common.h:871:对‘vtable for google::protobuf::internal::FunctionClosure0’未定义的引用
person.pb.o:在函数‘google::protobuf::GoogleOnceInit(long*, void (*)())’中:
/usr/include/google/protobuf/stubs/once.h:127:对‘google::protobuf::GoogleOnceInitImpl(long*, google::protobuf::Closure*)’未定义的引用
/usr/include/google/protobuf/stubs/once.h:126:对‘google::protobuf::internal::FunctionClosure0::~FunctionClosure0()’未定义的引用
/usr/include/google/protobuf/stubs/once.h:126:对‘google::protobuf::internal::FunctionClosure0::~FunctionClosure0()’未定义的引用
person.pb.o:在函数‘google::protobuf::internal::GetEmptyString[abi:cxx11]()’中:
/usr/include/google/protobuf/generated_message_util.h:84:对‘google::protobuf::internal::InitEmptyString()’未定义的引用
/usr/include/google/protobuf/generated_message_util.h:84:对‘google::protobuf::internal::empty_string_once_init_’未定义的引用
person.pb.o:在函数‘google::protobuf::MessageLite::MessageLite()’中:
/usr/include/google/protobuf/message_lite.h:79:对‘vtable for google::protobuf::MessageLite’未定义的引用
person.pb.o:在函数‘google::protobuf::Message::Message()’中:
/usr/include/google/protobuf/message.h:167:对‘vtable for google::protobuf::Message’未定义的引用
person.pb.o:在函数‘google::protobuf::UnknownFieldSet::Clear()’中:
/usr/include/google/protobuf/unknown_field_set.h:226:对‘google::protobuf::UnknownFieldSet::ClearFallback()’未定义的引用
person.pb.o:在函数‘google::protobuf::io::CodedInputStream::ReadVarint32(unsigned int*)’中:
/usr/include/google/protobuf/io/coded_stream.h:799:对‘google::protobuf::io::CodedInputStream::ReadVarint32Fallback(unsigned int*)’未定义的引用
person.pb.o:在函数‘google::protobuf::io::CodedOutputStream::WriteVarint32ToArray(unsigned int, unsigned char*)’中:
/usr/include/google/protobuf/io/coded_stream.h:1015:对‘google::protobuf::io::CodedOutputStream::WriteVarint32FallbackToArray(unsigned int, unsigned char*)’未定义的引用
person.pb.o:在函数‘google::protobuf::io::CodedOutputStream::WriteVarint32SignExtendedToArray(int, unsigned char*)’中:
/usr/include/google/protobuf/io/coded_stream.h:1030:对‘google::protobuf::io::CodedOutputStream::WriteVarint64ToArray(unsigned long, unsigned char*)’未定义的引用
person.pb.o:在函数‘google::protobuf::io::CodedOutputStream::VarintSize32(unsigned int)’中:
Makefile:245: recipe for target '1102test_proto' failed
/usr/include/google/protobuf/io/coded_stream.h:1091:对‘google::protobuf::io::CodedOutputStream::VarintSize32Fallback(unsigned int)’未定义的引用
person.pb.o:在函数‘google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(char const*, int, google::protobuf::internal::WireFormat::Operation, char const*)’中:
/usr/include/google/protobuf/wire_format.h:327:对‘google::protobuf::internal::WireFormat::VerifyUTF8StringFallback(char const*, int, google::protobuf::internal::WireFormat::Operation, char const*)’未定义的引用
person.pb.o:在函数‘Test::Person const* google::protobuf::internal::dynamic_cast_if_available<Test::Person const*, google::protobuf::Message const*>(google::protobuf::Message const*)’中:
/usr/include/google/protobuf/generated_message_reflection.h:496:对‘typeinfo for google::protobuf::Message’未定义的引用
person.pb.o:(.data.rel.ro._ZTVN4Test6PersonE[_ZTVN4Test6PersonE]+0x20):对‘google::protobuf::Message::GetTypeName[abi:cxx11]() const’未定义的引用
person.pb.o:(.data.rel.ro._ZTVN4Test6PersonE[_ZTVN4Test6PersonE]+0x40):对‘google::protobuf::Message::InitializationErrorString[abi:cxx11]() const’未定义的引用
person.pb.o:(.data.rel.ro._ZTVN4Test6PersonE[_ZTVN4Test6PersonE]+0x48):对‘google::protobuf::Message::CheckTypeAndMergeFrom(google::protobuf::MessageLite const&)’未定义的引用
person.pb.o:(.data.rel.ro._ZTVN4Test6PersonE[_ZTVN4Test6PersonE]+0x88):对‘google::protobuf::Message::DiscardUnknownFields()’未定义的引用
person.pb.o:(.data.rel.ro._ZTVN4Test6PersonE[_ZTVN4Test6PersonE]+0x90):对‘google::protobuf::Message::SpaceUsed() const’未定义的引用
person.pb.o:(.data.rel.ro._ZTIN4Test6PersonE[_ZTIN4Test6PersonE]+0x10):对‘typeinfo for google::protobuf::Message’未定义的引用
collect2: error: ld returned 1 exit status
make: *** [1102test_proto] Error 1
17:01:43: 进程"/usr/bin/make"退出,退出代码 2 。
Error while building/deploying project 1102test_proto (kit: Desktop Qt 5.9.1 GCC 64bit)
When executing step "Make"
17:01:43: Elapsed time: 00:00.

https://www.cnblogs.com/gongxijun/p/8981086.html //跑成功了

这篇博客是自己定义了一个demo.proto,描述了数据格式:

文件名字://demo.proto

package caffe;

message Student
   {
      required int32 age = 1; //ID required 表示必要字段
      required string name = 2; //str 必要字段
      optional int32 grade = 3 ; //optional field 可选字段,可以有无,最多b
   }

message WorkDay{
    required bool isworker =1;
    required bool isjiaban =2; //是否算加班
}

message Teacher{
    required string name = 1 ;
    required WorkDay work = 2;  //是否工作
    optional int32  age = 3;

}

//班级
message Class{
       required string name = 1;   //班级名称
       repeated Student stu = 2;    //班级成员 数组
       repeated Teacher teacher=3;
   }

然后我们敲命令编译:

/usr/bin$ ./protoc -I=/media/d_2/everyday/1102/protoc/proto --cpp_out=/media/d_2/everyday/1102/protoc/my_out /media/d_2/everyday/1102/protoc/proto/demo.proto

会在my_out文件夹里面生成两个文件demo.pb.cc和demo.pb.h

作者提供了类似网络的prototxt

name: "三年级23班"

teacher {
  name: "tom"
  age: 17
  work {
    isworker: 1 ;#中文
    isjiaban: 1;
   }
}

stu {
    age: 19;
    name: "demo"; ##中文
    grade: 134;
}

stu {
    age: 19;
    name: "google"; ##中文
    grade: 134;
}

stu {
    age: 19;
    name: "snake"; ##中文
    grade: 134;
}

新建QT工程加载demo.pb.cc和demo.pb.h:main函数如下:

//
// Created by xijun1 on 2017/12/22.
//
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>

//反射机制
//#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/dynamic_message.h>

#include "demo.pb.h"
#include<iostream>
#include <fstream>
#include<ios>
#include <cstdlib>
#include <cstring>
#include <cstdio>

#include <fcntl.h>   // open
using namespace std;

void InfoStudents(const caffe::Student & stu){
    cout<< "student info:"<<endl;
    cout<<"     name: "<<stu.name()<<endl;
    cout<<"     age: "<<stu.age()<<endl;
    cout<<"     grade: "<<stu.grade()<<endl;
}

void InfoTeacher(const caffe::Teacher & teacher) {
    cout << "teacher info:" << endl;
    cout << "       name: " << teacher.name() << endl;
    cout << "       age: " << teacher.age() << endl;
    cout<< "            is worker: "<<teacher.work().isworker()<<endl;
    cout<< "            is jiaban: "<<teacher.work().isjiaban()<<endl;
}


int main(void)
{
    caffe::Class cls;
    int file_desc = open("/media/d_2/everyday/1102/protoc/param.prototxt",O_NDELAY);

    google::protobuf::io::FileInputStream fileInputStream(file_desc);
    if(!google::protobuf::TextFormat::Parse(&fileInputStream,&cls)){
        std::cout<<"parse failure."<<std::endl;
        return -1;
    }
    std::cout<<cls.name()<<std::endl;


    //按照索引进行读取
    for(int i=1;i<cls.GetMetadata().descriptor->field_count(); ++i){
        std::cout<<cls.descriptor()->field(i)->name()<<std::endl;
        //cout<<cls.descriptor()->field(i)->full_name()<<endl;
        if(cls.descriptor()->field(i)->name()=="stu"){
            for (auto &stu_info : cls.stu()){

                 InfoStudents(stu_info);
            }
        }

        if(cls.descriptor()->field(i)->name()=="teacher"){
            for (auto &teacher_info : cls.teacher()){

                InfoTeacher(teacher_info);
            }
        }
    }

    return 0;
}

终端输出如下:

三年级23班
stu
student info:
     name: demo
     age: 19
     grade: 134
student info:
     name: google
     age: 19
     grade: 134
student info:
     name: snake
     age: 19
     grade: 134
teacher
teacher info:
       name: tom
       age: 17
            is worker: 1
            is jiaban: 1
按 <RETURN> 来关闭窗口...

工程文件压缩包:https://download.csdn.net/download/yang332233/10765029

下一步读这个博客:https://blog.csdn.net/qiusuoxiaozi/article/details/61199892

http://hexiecs.com/2017/08/12/others/protobuf/

猜你喜欢

转载自blog.csdn.net/yang332233/article/details/83657735