g++ 编译错误:使用protobuf undefined 错误

用protobuf生成.pb.cc 和 .pb.h文件后,在程序文件暗中引用.pb.h文件,运行++

时总会出现undefined 错误 如下

lc@lc-host:~/PycharmProjects/cplus/proto_bazel_test$ g++ main.cpp msg.pb.cc -o main.out -lprotobuf
/usr/bin/ld: /tmp/ccxuZhla.o: in function `descriptor_table_msg_2eproto_metadata_getter(int)':
msg.pb.cc:(.text+0x1c): undefined reference to `google::protobuf::internal::AssignDescriptors(google::protobuf::internal::DescriptorTable const*, bool)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `Im::Content::Content(Im::Content const&)':
msg.pb.cc:(.text+0x21c): undefined reference to `google::protobuf::internal::ArenaStringPtr::Set(google::protobuf::internal::ArenaStringPtr::EmptyDefault, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::Arena*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `Im::Content::Clear()':
msg.pb.cc:(.text+0x355): undefined reference to `google::protobuf::internal::ArenaStringPtr::ClearToEmpty()'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `Im::Content::_InternalParse(char const*, google::protobuf::internal::ParseContext*)':
msg.pb.cc:(.text+0x4ce): 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*)'
/usr/bin/ld: msg.pb.cc:(.text+0x5f5): undefined reference to `google::protobuf::internal::UnknownFieldParse(unsigned long, google::protobuf::UnknownFieldSet*, char const*, google::protobuf::internal::ParseContext*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `Im::Content::_InternalSerialize(unsigned char*, google::protobuf::io::EpsCopyOutputStream*) const':
msg.pb.cc:(.text+0x9bb): undefined reference to `google::protobuf::internal::WireFormat::InternalSerializeUnknownFieldsToArray(google::protobuf::UnknownFieldSet const&, unsigned char*, google::protobuf::io::EpsCopyOutputStream*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `Im::Content::ByteSizeLong() const':
msg.pb.cc:(.text+0xadd): undefined reference to `google::protobuf::internal::ComputeUnknownFieldsSize(google::protobuf::internal::InternalMetadata const&, unsigned long, google::protobuf::internal::CachedSize*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `Im::Content::InternalSwap(Im::Content*)':
msg.pb.cc:(.text+0x10cf): undefined reference to `google::protobuf::internal::ArenaStringPtr::Mutable[abi:cxx11](google::protobuf::internal::ArenaStringPtr::EmptyDefault, google::protobuf::Arena*)'
/usr/bin/ld: msg.pb.cc:(.text+0x10e9): undefined reference to `google::protobuf::internal::ArenaStringPtr::Mutable[abi:cxx11](google::protobuf::internal::ArenaStringPtr::EmptyDefault, google::protobuf::Arena*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `Im::Content* google::protobuf::Arena::CreateMaybeMessage<Im::Content>(google::protobuf::Arena*)':
msg.pb.cc:(.text+0x123a): undefined reference to `google::protobuf::Arena::AllocateAlignedWithCleanup(unsigned long, std::type_info const*)'
/usr/bin/ld: msg.pb.cc:(.text+0x1281): undefined reference to `google::protobuf::Arena::AllocateAlignedWithCleanup(unsigned long, std::type_info const*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `__static_initialization_and_destruction_0(int, int)':
msg.pb.cc:(.text+0x13a9): undefined reference to `google::protobuf::internal::AddDescriptorsRunner::AddDescriptorsRunner(google::protobuf::internal::DescriptorTable const*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `google::protobuf::io::EpsCopyOutputStream::EnsureSpace(unsigned char*)':
msg.pb.cc:(.text._ZN6google8protobuf2io19EpsCopyOutputStream11EnsureSpaceEPh[_ZN6google8protobuf2io19EpsCopyOutputStream11EnsureSpaceEPh]+0x39): undefined reference to `google::protobuf::io::EpsCopyOutputStream::EnsureSpaceFallback(unsigned char*)'
/usr/bin/ld: /tmp/ccxuZhla.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*)':
msg.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*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `google::protobuf::Arena::AllocateAlignedWithHook(unsigned long, unsigned long, std::type_info const*)':
msg.pb.cc:(.text._ZN6google8protobuf5Arena23AllocateAlignedWithHookEmmPKSt9type_info[_ZN6google8protobuf5Arena23AllocateAlignedWithHookEmmPKSt9type_info]+0x41): undefined reference to `google::protobuf::Arena::AllocateAlignedWithHook(unsigned long, std::type_info const*)'
/usr/bin/ld: msg.pb.cc:(.text._ZN6google8protobuf5Arena23AllocateAlignedWithHookEmmPKSt9type_info[_ZN6google8protobuf5Arena23AllocateAlignedWithHookEmmPKSt9type_info]+0x65): undefined reference to `google::protobuf::Arena::AllocateAlignedWithHook(unsigned long, std::type_info const*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `google::protobuf::internal::EpsCopyInputStream::DoneWithCheck(char const**, int)':
msg.pb.cc:(.text._ZN6google8protobuf8internal18EpsCopyInputStream13DoneWithCheckEPPKci[_ZN6google8protobuf8internal18EpsCopyInputStream13DoneWithCheckEPPKci]+0x178): undefined reference to `google::protobuf::internal::EpsCopyInputStream::DoneFallback(int, int)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `google::protobuf::internal::VarintParseSlow(char const*, unsigned int, unsigned long*)':
msg.pb.cc:(.text._ZN6google8protobuf8internal15VarintParseSlowEPKcjPm[_ZN6google8protobuf8internal15VarintParseSlowEPKcjPm]+0x24): undefined reference to `google::protobuf::internal::VarintParseSlow64(char const*, unsigned int)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `google::protobuf::internal::ReadTag(char const*, unsigned int*, unsigned int)':
msg.pb.cc:(.text._ZN6google8protobuf8internal7ReadTagEPKcPjj[_ZN6google8protobuf8internal7ReadTagEPKcPjj]+0x80): undefined reference to `google::protobuf::internal::ReadTagFallback(char const*, unsigned int)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `google::protobuf::internal::VerifyUTF8(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const*, char const*)':
msg.pb.cc:(.text._ZN6google8protobuf8internal10VerifyUTF8EPKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPKc[_ZN6google8protobuf8internal10VerifyUTF8EPKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPKc]+0x49): undefined reference to `google::protobuf::internal::VerifyUTF8(google::protobuf::StringPiece, char const*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `Im::Content::_internal_set_str(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
msg.pb.cc:(.text._ZN2Im7Content17_internal_set_strERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN2Im7Content17_internal_set_strERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x46): undefined reference to `google::protobuf::internal::ArenaStringPtr::Set(google::protobuf::internal::ArenaStringPtr::EmptyDefault, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::Arena*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `Im::Content::_internal_mutable_str[abi:cxx11]()':
msg.pb.cc:(.text._ZN2Im7Content21_internal_mutable_strB5cxx11Ev[_ZN2Im7Content21_internal_mutable_strB5cxx11Ev]+0x3b): undefined reference to `google::protobuf::internal::ArenaStringPtr::Mutable[abi:cxx11](google::protobuf::internal::ArenaStringPtr::EmptyDefault, google::protobuf::Arena*)'
/usr/bin/ld: /tmp/ccxuZhla.o: in function `google::protobuf::UnknownFieldSet* google::protobuf::internal::InternalMetadata::mutable_unknown_fields_slow<google::protobuf::UnknownFieldSet>()':
msg.pb.cc:(.text._ZN6google8protobuf8internal16InternalMetadata27mutable_unknown_fields_slowINS0_15UnknownFieldSetEEEPT_v[_ZN6google8protobuf8internal16InternalMetadata27mutable_unknown_fields_slowINS0_15UnknownFieldSetEEEPT_v]+0x219): undefined reference to `google::protobuf::Arena::AllocateAlignedWithCleanup(unsigned long, std::type_info const*)'
/usr/bin/ld: msg.pb.cc:(.text._ZN6google8protobuf8internal16InternalMetadata27mutable_unknown_fields_slowINS0_15UnknownFieldSetEEEPT_v[_ZN6google8protobuf8internal16InternalMetadata27mutable_unknown_fields_slowINS0_15UnknownFieldSetEEEPT_v]+0x272): undefined reference to `google::protobuf::Arena::AllocateAlignedWithCleanup(unsigned long, std::type_info const*)'
collect2: error: ld returned 1 exit status

搜了很多资料,终于解决了,方法是 需要生成动态连接库如下

lc@lc-host:~/PycharmProjects/cplus/proto_bazel_test$ ll
总用量 60
drwxrwxr-x 5 lc lc  4096 7月  28 14:31 ./
drwxrwxr-x 9 lc lc  4096 7月  28 13:35 ../
-rw-rw-r-- 1 lc lc   303 7月  28 14:24 main.cpp
-rw-rw-r-- 1 lc lc 12906 7月  28 13:54 msg.pb.cc
-rw-rw-r-- 1 lc lc 11943 7月  28 13:54 msg.pb.h
drwxrwxr-x 2 lc lc  4096 7月  28 12:20 OK_proto_bazel/
-rw-rw-r-- 1 lc lc   103 7月  28 14:09 speak.cpp
-rw-rw-r-- 1 lc lc    99 7月  28 13:26 speak.h
drwxrwxr-x 2 lc lc  4096 7月  26 18:06 test_prot_bazel2/
drwxrwxr-x 2 lc lc  4096 7月  28 12:20 .vscode/
lc@lc-host:~/PycharmProjects/cplus/proto_bazel_test$ g++ -c main.cpp & g++ -c msg.pb.cc 
[1] 80671
[1]+  已完成               g++ -c main.cpp
lc@lc-host:~/PycharmProjects/cplus/proto_bazel_test$ g++ ./main.o msg.pb.o -o main_test.out `pkg-config --cflags --libs protobuf`
lc@lc-host:~/PycharmProjects/cplus/proto_bazel_test$ ./main_test.out 
123
hello
lc@lc-host:~/PycharmProjects/cplus/proto_bazel_test$ 

详细请参考 

https://blog.csdn.net/xiamentingtao/article/details/78339423

https://www.jianshu.com/p/efc9167e7044

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值