首页 > 其他分享 >解决undefined reference to `google::protobuf::MessageLite::SerializeToString(std::string*)问题

解决undefined reference to `google::protobuf::MessageLite::SerializeToString(std::string*)问题

时间:2024-10-07 16:49:13浏览次数:10  
标签:std google const undefined reference contacts internal protobuf

按照如下步骤安装了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博客

protobuf版本常见问题-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

标签:std,google,const,undefined,reference,contacts,internal,protobuf
From: https://blog.csdn.net/flappy_immortal/article/details/142740599

相关文章

  • `std::future`--异步的优势
    std::future相比于直接使用线程在C++中有几个重要的优势,主要体现在同步结果获取、简化代码管理、以及更安全的异步任务管理等方面。以下是std::future的一些主要优势:1.自动结果获取与同步std::future提供了一种便捷的机制来获取异步任务的返回值。当我们使用线程时,通常......
  • std::packaged_task<返回类型(参数类型)>
    std::packaged_task概述std::packaged_task是C++11引入的标准库模板类,用于包装一个可调用对象(如函数、lambda表达式、函数对象等),使其能够与std::future协同工作。简单来说,它将一个任务(可调用对象)包装起来,并允许你获取该任务的执行结果,这样你可以在一个线程中执行任务,并在......
  • std::make_shared
    std::make_shared是C++11引入的一个标准库函数,用于创建一个std::shared_ptr,并在堆上分配所需的对象。它的功能是将对象的创建和shared_ptr的初始化合并在一起,提高了效率和安全性。使用方法:autoptr=std::make_shared<T>(args...);T:共享指针所管理的对象的类型。a......
  • `std::packaged_task`、`std::thread` 和 `std::async` 的区别与联系
    std::packaged_task、std::thread和std::async的区别与联系std::packaged_task、std::thread和std::async都是C++11中提供的并发工具,用于执行任务并处理多线程操作。虽然它们都有类似的作用(并发执行任务),但在功能和使用方式上有显著区别。下面分别解释它们的特点,并说明它......
  • std::unique_lock
    std::unique_lock是C++11标准库中的一个类,提供了一种灵活的方式来管理互斥量(mutex)。它比std::lock_guard更加灵活,允许在不同的作用域和不同的锁定策略之间进行选择。以下是对unique_lock的详细解释,包括其用途、使用方法和优点。1.定义std::unique_lock是一种RAII(资......
  • google chrome去除组织策略
    https://groups.google.com/g/huddcaricang/c/Bahkeds6vrg?pli=1https://hackerdose.com/downloads/utility/chrome-policy-remover/::ChromePolicyRemoverforWindows::version1.0-21May2022::CreatedbyStefanvd::ChromeProductExpert::https://prod......
  • 程序运行异常: Undefined constant"PAGE
    在使用PbootCMS时,如果遇到“Undefinedconstant'PAGE'”的错误,尤其是在后台自定义表单编辑字段时,这通常是由于PHP版本不兼容导致的问题。具体来说,某些常量或函数可能在较高版本的PHP中不再支持或行为有所变化。解决方法降低PHP版本:将PHP版本从8.0降到7.3。详细步骤检......
  • GoogLeNet训练CIFAR10[Pytorch+训练信息+.pth文件]
    0引言GoogLeNet,它是一种深度卷积神经网络,由Google研究人员在2014年提出,用于图像识别任务。CIFAR-10是一个常用的图像识别数据集,包含10个类别,每个类别有6000张32x32的彩色图像。本文使用Pycharm及Pytorch框架搭建GoogLeNet神经网络框架,使用CIFAR10数据集训练模型。笔者查阅资......
  • stdio流缓冲区
    文件流缓冲区每个进程都各自维护一个文件流表,和文件描述符表类似,并且这些文件流都有自己对应的缓冲区缓冲区的设计就是为了减少磁盘io,加快io的速度缓冲区模式通过setvbuf或者setbuf两个函数设置缓冲区的模式无缓冲区就是禁用缓冲区的意思,这种模式下,就相当于直接调用write函......
  • Google AlphaChip改变了计算机芯片设计
      每周跟踪AI热点新闻动向和震撼发展想要探索生成式人工智能的前沿进展吗?订阅我们的简报,深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同,从行业内部的深度分析和实用指南中受益。不要错过这个机会,成为AI领域的领跑者。点击订阅,与未来同行!订阅:https://......