按照如下步骤安装了proto:
https://zhuanlan.zhihu.com/p/631291781
但是在后续的protoBuf 测试demo中出现了问题
root@e23598ae2d28:/home/lee/Code/protof_test# g++ test.cc contacts.pb.cc -o test_proto -lprotobuf -std=c++11 -lpthread
/tmp/ccbTc1bj.o: In function `google::protobuf::internal::ArenaStringPtr::Set(char const*, google::protobuf::Arena*)':
test.cc:(.text._ZN6google8protobuf8internal14ArenaStringPtr3SetEPKcPNS0_5ArenaE[_ZN6google8protobuf8internal14ArenaStringPtr3SetEPKcPNS0_5ArenaE]+0x5a): undefined reference to `google::protobuf::internal::ArenaStringPtr::Set(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::Arena*)'
/tmp/ccTq7NRn.o: In function `contacts::PeopleInfo::PeopleInfo(google::protobuf::internal::ConstantInitialized)':
contacts.pb.cc:(.text+0x2d): undefined reference to `google::protobuf::internal::fixed_address_empty_string[abi:cxx11]'
/tmp/ccTq7NRn.o: In function `contacts::PeopleInfo::PeopleInfo(contacts::PeopleInfo const&)':
contacts.pb.cc:(.text+0x187): undefined reference to `google::protobuf::internal::ArenaStringPtr::Set(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::Arena*)'
/tmp/ccTq7NRn.o: In function `contacts::PeopleInfo::Clear()':
contacts.pb.cc:(.text+0x291): undefined reference to `google::protobuf::internal::ArenaStringPtr::ClearToEmpty()'
/tmp/ccTq7NRn.o: In function `contacts::PeopleInfo::_InternalParse(char const*, google::protobuf::internal::ParseContext*)':
contacts.pb.cc:(.text+0x362): undefined reference to `google::protobuf::internal::InlineGreedyStringParser(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, char const*, google::protobuf::internal::ParseContext*)'
contacts.pb.cc:(.text+0x44e): undefined reference to `google::protobuf::internal::UnknownFieldParse(unsigned long, google::protobuf::UnknownFieldSet*, char const*, google::protobuf::internal::ParseContext*)'
/tmp/ccTq7NRn.o: In function `contacts::PeopleInfo::_InternalSerialize(unsigned char*, google::protobuf::io::EpsCopyOutputStream*) const':
contacts.pb.cc:(.text+0x5e7): undefined reference to `google::protobuf::internal::WireFormat::InternalSerializeUnknownFieldsToArray(google::protobuf::UnknownFieldSet const&, unsigned char*, google::protobuf::io::EpsCopyOutputStream*)'
/tmp/ccTq7NRn.o: In function `contacts::PeopleInfo::ByteSizeLong() const':
contacts.pb.cc:(.text+0x68f): undefined reference to `google::protobuf::Message::MaybeComputeUnknownFieldsSize(unsigned long, google::protobuf::internal::CachedSize*) const'
/tmp/ccTq7NRn.o: In function `contacts::PeopleInfo::MergeImpl(google::protobuf::Message&, google::protobuf::Message const&)':
contacts.pb.cc:(.text+0x7ab): undefined reference to `google::protobuf::internal::ArenaStringPtr::Set(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::Arena*)'
/tmp/ccTq7NRn.o: In function `contacts::PeopleInfo::GetMetadata() const':
contacts.pb.cc:(.text+0x93a): undefined reference to `google::protobuf::internal::AssignDescriptors(google::protobuf::internal::DescriptorTable const* (*)(), std::once_flag*, google::protobuf::Metadata const&)'
/tmp/ccTq7NRn.o: In function `__static_initialization_and_destruction_0(int, int)':
contacts.pb.cc:(.text+0x9e1): undefined reference to `google::protobuf::internal::AddDescriptorsRunner::AddDescriptorsRunner(google::protobuf::internal::DescriptorTable const*)'
/tmp/ccTq7NRn.o: In function `google::protobuf::stringpiece_internal::StringPiece::CheckSize(unsigned long)':
contacts.pb.cc:(.text._ZN6google8protobuf20stringpiece_internal11StringPiece9CheckSizeEm[_ZN6google8protobuf20stringpiece_internal11StringPiece9CheckSizeEm]+0x2b): undefined reference to `google::protobuf::stringpiece_internal::StringPiece::LogFatalSizeTooBig(unsigned long, char const*)'
/tmp/ccTq7NRn.o: In function `google::protobuf::io::EpsCopyOutputStream::EnsureSpace(unsigned char*)':
contacts.pb.cc:(.text._ZN6google8protobuf2io19EpsCopyOutputStream11EnsureSpaceEPh[_ZN6google8protobuf2io19EpsCopyOutputStream11EnsureSpaceEPh]+0x35): undefined reference to `google::protobuf::io::EpsCopyOutputStream::EnsureSpaceFallback(unsigned char*)'
/tmp/ccTq7NRn.o: In function `google::protobuf::io::EpsCopyOutputStream::WriteStringMaybeAliased(unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)':
contacts.pb.cc:(.text._ZN6google8protobuf2io19EpsCopyOutputStream23WriteStringMaybeAliasedEjRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPh[_ZN6google8protobuf2io19EpsCopyOutputStream23WriteStringMaybeAliasedEjRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPh]+0x9b): undefined reference to `google::protobuf::io::EpsCopyOutputStream::WriteStringMaybeAliasedOutline(unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)'
/tmp/ccTq7NRn.o: In function `google::protobuf::Arena::AllocateInternal(unsigned long, unsigned long, void (*)(void*), std::type_info const*)':
contacts.pb.cc:(.text._ZN6google8protobuf5Arena16AllocateInternalEmmPFvPvEPKSt9type_info[_ZN6google8protobuf5Arena16AllocateInternalEmmPFvPvEPKSt9type_info]+0x65): undefined reference to `google::protobuf::Arena::AllocateAlignedWithCleanup(unsigned long, std::type_info const*)'
contacts.pb.cc:(.text._ZN6google8protobuf5Arena16AllocateInternalEmmPFvPvEPKSt9type_info[_ZN6google8protobuf5Arena16AllocateInternalEmmPFvPvEPKSt9type_info]+0xac): undefined reference to `google::protobuf::Arena::AllocateAlignedWithCleanup(unsigned long, std::type_info const*)'
/tmp/ccTq7NRn.o: In function `google::protobuf::Arena::AllocateAlignedWithHook(unsigned long, unsigned long, std::type_info const*)':
contacts.pb.cc:(.text._ZN6google8protobuf5Arena23AllocateAlignedWithHookEmmPKSt9type_info[_ZN6google8protobuf5Arena23AllocateAlignedWithHookEmmPKSt9type_info]+0x3d): undefined reference to `google::protobuf::Arena::AllocateAlignedWithHook(unsigned long, std::type_info const*)'
contacts.pb.cc:(.text._ZN6google8protobuf5Arena23AllocateAlignedWithHookEmmPKSt9type_info[_ZN6google8protobuf5Arena23AllocateAlignedWithHookEmmPKSt9type_info]+0x61): undefined reference to `google::protobuf::Arena::AllocateAlignedWithHook(unsigned long, std::type_info const*)'
/tmp/ccTq7NRn.o: In function `google::protobuf::internal::ArenaStringPtr::InitDefault()':
contacts.pb.cc:(.text._ZN6google8protobuf8internal14ArenaStringPtr11InitDefaultEv[_ZN6google8protobuf8internal14ArenaStringPtr11InitDefaultEv]+0x22): undefined reference to `google::protobuf::internal::fixed_address_empty_string[abi:cxx11]'
/tmp/ccTq7NRn.o: In function `google::protobuf::internal::InternalMetadata::~InternalMetadata()':
contacts.pb.cc:(.text._ZN6google8protobuf8internal16InternalMetadataD2Ev[_ZN6google8protobuf8internal16InternalMetadataD5Ev]+0x14): undefined reference to `google::protobuf::internal::InternalMetadata::CheckedDestruct()'
/tmp/ccTq7NRn.o: In function `google::protobuf::internal::EpsCopyInputStream::DoneWithCheck(char const**, int)':
contacts.pb.cc:(.text._ZN6google8protobuf8internal18EpsCopyInputStream13DoneWithCheckEPPKci[_ZN6google8protobuf8internal18EpsCopyInputStream13DoneWithCheckEPPKci]+0x177): undefined reference to `google::protobuf::internal::EpsCopyInputStream::DoneFallback(int, int)'
/tmp/ccTq7NRn.o: In function `google::protobuf::internal::VarintParseSlow(char const*, unsigned int, unsigned int*)':
contacts.pb.cc:(.text._ZN6google8protobuf8internal15VarintParseSlowEPKcjPj[_ZN6google8protobuf8internal15VarintParseSlowEPKcjPj]+0x20): undefined reference to `google::protobuf::internal::VarintParseSlow32(char const*, unsigned int)'
/tmp/ccTq7NRn.o: In function `google::protobuf::internal::ReadTag(char const*, unsigned int*, unsigned int)':
contacts.pb.cc:(.text._ZN6google8protobuf8internal7ReadTagEPKcPjj[_ZN6google8protobuf8internal7ReadTagEPKcPjj]+0x7c): undefined reference to `google::protobuf::internal::ReadTagFallback(char const*, unsigned int)'
/tmp/ccTq7NRn.o: In function `google::protobuf::internal::VerifyUTF8(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const*, char const*)':
contacts.pb.cc:(.text._ZN6google8protobuf8internal10VerifyUTF8EPKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPKc[_ZN6google8protobuf8internal10VerifyUTF8EPKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPKc]+0x45): undefined reference to `google::protobuf::internal::VerifyUTF8(google::protobuf::stringpiece_internal::StringPiece, char const*)'
/tmp/ccTq7NRn.o: In function `contacts::PeopleInfo::_internal_mutable_name[abi:cxx11]()':
contacts.pb.cc:(.text._ZN8contacts10PeopleInfo22_internal_mutable_nameB5cxx11Ev[_ZN8contacts10PeopleInfo22_internal_mutable_nameB5cxx11Ev]+0x28): undefined reference to `google::protobuf::internal::ArenaStringPtr::Mutable[abi:cxx11](google::protobuf::Arena*)'
/tmp/ccTq7NRn.o: In function `contacts::PeopleInfo::SharedDtor()':
contacts.pb.cc:(.text._ZN8contacts10PeopleInfo10SharedDtorEv[_ZN8contacts10PeopleInfo10SharedDtorEv]+0x97): undefined reference to `google::protobuf::internal::ArenaStringPtr::Destroy()'
/tmp/ccTq7NRn.o:(.data.rel.ro+0x0): undefined reference to `google::protobuf::Message::CopyWithSourceCheck(google::protobuf::Message&, google::protobuf::Message const&)'
/tmp/ccTq7NRn.o:(.data.rel.ro._ZTVN8contacts10PeopleInfoE[_ZTVN8contacts10PeopleInfoE]+0x80): undefined reference to `google::protobuf::Message::SpaceUsedLong() const'
collect2: error: ld returned 1 exit status
网上有些说是protof编译的时候的gcc的版本以及说是protof可能系统自带了一个造成的冲突:
解决protobuf: undefined reference to `google::protobuf::internal::fixed_address_empty_string[abi:cxx11_undefined reference abi:cxx11-CSDN博客
不过我看了下我只装了一个版本的。
解决办法:
g++ test.cc contacts.pb.cc -o test_proto $(pkg-config --cflags --libs protobuf) -std=c++11 -lpthread
暂时这么解决了,demo不报错了,如果有更好的方法欢迎交流,因为我对linux这一块儿也是完全不熟。
root@e23598ae2d28:/home/lee/Code/protof_test# ./test_proto
序列化成功,结果:
张珊
反序列化成功!
姓名: 张珊
年龄: 20