xcode c++ 编程使用 protobuf

此文的着重点是 protobuf 的实际应用,并解决碰到的问题,不会过多的介绍 protobuf 本身。

1、我当前使用的 mac OS 系统是:

 macOS Mojave

Version 10.14.6

2、Command Line Tools:

Xcode Version 11.3.1 (11C504)

3、安装 protobuf ,

如果你愿意,可以选择源码安装 protobuf ,

我的开发环境采用 port Package Manager 管理维护,所以 port 安装(Centos 下使用 yum ,Ubuntu 使用 sudo apt-get),安装 protobuf-c protobuf3-cpp ,执行命令:

macbook-pro:~ username$ sudo port install protobuf-c

--->  Computing dependencies for protobuf-c

The following dependencies will be installed:  protobuf3-cpp

Continue? [Y/n]: Y

--->  Fetching archive for protobuf3-cpp

--->  Attempting to fetch protobuf3-cpp-3.17.3_0.darwin_18.x86_64.tbz2 from https://pek.cn.packages.macports.org/macports/packages/protobuf3-cpp

--->  Attempting to fetch protobuf3-cpp-3.17.3_0.darwin_18.x86_64.tbz2.rmd160 from https://pek.cn.packages.macports.org/macports/packages/protobuf3-cpp

--->  Installing protobuf3-cpp @3.17.3_0

--->  Activating protobuf3-cpp @3.17.3_0

--->  Cleaning protobuf3-cpp

--->  Fetching archive for protobuf-c

--->  Attempting to fetch protobuf-c-1.4.0_0.darwin_18.x86_64.tbz2 from https://pek.cn.packages.macports.org/macports/packages/protobuf-c

--->  Attempting to fetch protobuf-c-1.4.0_0.darwin_18.x86_64.tbz2.rmd160 from https://pek.cn.packages.macports.org/macports/packages/protobuf-c

--->  Installing protobuf-c @1.4.0_0

--->  Activating protobuf-c @1.4.0_0

--->  Cleaning protobuf-c

--->  Scanning binaries for linking errors

--->  No broken files found.                             

--->  No broken ports found.

4、使用 protoc 编译 .proto 文件,

        成功安装后,就可以使用 protoc 命令,利用 Protobuf 编译器将 .proto 编译成目标语言(这里解说的是 c++ )。

编写好我的 rpc.proto 文件, rpc.proto  文件位置目录: ~/cs/cpp/UseQueue/src/protobuf/ 。

把将要生成的目标语言文件也放置到此目录,执行命令,

macbook-pro:~ username$ protoc -I=~/cs/cpp/UseQueue/src/protobuf/ --cpp_out=~/cs/cpp/UseQueue/src/protobuf/ ~/cs/cpp/UseQueue/src/protobuf/rpc.proto

此命令生成两个文件 rpc.pb.cc C++类的实现文件和 rpc.pb.h 类的头文件,将生成文件引入到工程,编译,报错:

Undefined symbols for architecture x86_64: 

  "google::protobuf::RpcChannel::~RpcChannel()", referenced from:

      net::RpcChannel::RpcChannel() in RpcChannel.o

      net::RpcChannel::RpcChannel(std::__1::shared_ptr<net::TcpConnection> const&) in RpcChannel.o

      net::RpcChannel::~RpcChannel() in RpcChannel.o

  "google::protobuf::MessageLite::ParseFromArray(void const*, int)", referenced from:

      net::ProtobufCodecLite::parseFromBuffer(StringPiece, google::protobuf::Message*) in ProtobufCodecLite.o

  "google::protobuf::MessageLite::ParseFromString(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:

      net::RpcChannel::onRpcMessage(std::__1::shared_ptr<net::TcpConnection> const&, std::__1::shared_ptr<net::RpcMessage> const&, Timestamp) in RpcChannel.o

  "google::protobuf::UnknownFieldSet::ClearFallback()", referenced from:

      google::protobuf::UnknownFieldSet::Clear() in rpc.pb.o

  "google::protobuf::UnknownFieldSet::default_instance()", referenced from:

      net::RpcMessage::_InternalSerialize(unsigned char*, google::protobuf::io::EpsCopyOutputStream*) const in rpc.pb.o

  "google::protobuf::UnknownFieldSet::AddVarint(int, unsigned long long)", referenced from:

      google::protobuf::internal::WriteVarint(unsigned int, unsigned long long, google::protobuf::UnknownFieldSet*) in rpc.pb.o

  "google::protobuf::UnknownFieldSet::MergeFrom(google::protobuf::UnknownFieldSet const&)", referenced from:

      void google::protobuf::internal::InternalMetadata::DoMergeFrom<google::protobuf::UnknownFieldSet>(google::protobuf::UnknownFieldSet const&) in rpc.pb.o

  "google::protobuf::stringpiece_internal::StringPiece::LogFatalSizeTooBig(unsigned long, char const*)", referenced from:

      google::protobuf::stringpiece_internal::StringPiece::CheckSize(unsigned long) in rpc.pb.o

  "google::protobuf::io::EpsCopyOutputStream::EnsureSpaceFallback(unsigned char*)", referenced from:

      google::protobuf::io::EpsCopyOutputStream::EnsureSpace(unsigned char*) in rpc.pb.o

  "google::protobuf::io::EpsCopyOutputStream::WriteStringMaybeAliasedOutline(unsigned int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned char*)", referenced from:

      google::protobuf::io::EpsCopyOutputStream::WriteStringMaybeAliased(unsigned int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned char*) in rpc.pb.o

  "google::protobuf::Arena::AllocateAlignedWithHook(unsigned long, std::type_info const*)", referenced from:

      google::protobuf::Arena::AllocateAlignedWithHook(unsigned long, unsigned long, std::type_info const*) in rpc.pb.o

  "google::protobuf::Arena::AllocateAlignedWithCleanup(unsigned long, std::type_info const*)", referenced from:

      google::protobuf::Arena::AllocateInternal(unsigned long, unsigned long, void (*)(void*), std::type_info const*) in rpc.pb.o

  "google::protobuf::Closure::~Closure()", referenced from:

      google::protobuf::internal::MethodClosure2<net::RpcChannel, google::protobuf::Message*, long long>::~MethodClosure2() in RpcChannel.o

  "google::protobuf::Message::CopyWithSizeCheck(google::protobuf::Message*, google::protobuf::Message const&)", referenced from:

      net::RpcMessage::_class_data_ in rpc.pb.o

  "google::protobuf::Message::CheckTypeAndMergeFrom(google::protobuf::MessageLite const&)", referenced from:

      vtable for net::RpcMessage in rpc.pb.o

  "google::protobuf::Message::CopyFrom(google::protobuf::Message const&)", referenced from:

      vtable for net::RpcMessage in rpc.pb.o

  "google::protobuf::Message::MergeFrom(google::protobuf::Message const&)", referenced from:

      vtable for net::RpcMessage in rpc.pb.o

  "google::protobuf::internal::LogMessage::LogMessage(google::protobuf::LogLevel, char const*, int)", referenced from:

      net::ProtobufCodecLite::serializeToBuffer(google::protobuf::Message const&, net::Buffer*) in ProtobufCodecLite.o

      google::protobuf::internal::ToIntSize(unsigned long) in ProtobufCodecLite.o

      ByteSizeConsistencyError(int, int, int) in ProtobufCodecLite.o

      net::RpcMessage::SharedDtor() in rpc.pb.o

      net::RpcMessage::MergeFrom(net::RpcMessage const&) in rpc.pb.o

      google::protobuf::internal::InternalMetadata::InternalMetadata(google::protobuf::Arena*, bool) in rpc.pb.o

      google::protobuf::internal::EpsCopyInputStream::DoneWithCheck(char const**, int) in rpc.pb.o

      ...

  "google::protobuf::internal::LogMessage::~LogMessage()", referenced from:

      net::ProtobufCodecLite::serializeToBuffer(google::protobuf::Message const&, net::Buffer*) in ProtobufCodecLite.o

      google::protobuf::internal::ToIntSize(unsigned long) in ProtobufCodecLite.o

      ByteSizeConsistencyError(int, int, int) in ProtobufCodecLite.o

      net::RpcMessage::SharedDtor() in rpc.pb.o

      net::RpcMessage::MergeFrom(net::RpcMessage const&) in rpc.pb.o

      google::protobuf::internal::InternalMetadata::InternalMetadata(google::protobuf::Arena*, bool) in rpc.pb.o

      google::protobuf::internal::EpsCopyInputStream::DoneWithCheck(char const**, int) in rpc.pb.o

      ...

  "google::protobuf::internal::LogMessage::operator<<(char const*)", referenced from:

      net::ProtobufCodecLite::serializeToBuffer(google::protobuf::Message const&, net::Buffer*) in ProtobufCodecLite.o

      google::protobuf::internal::ToIntSize(unsigned long) in ProtobufCodecLite.o

      ByteSizeConsistencyError(int, int, int) in ProtobufCodecLite.o

      net::RpcMessage::SharedDtor() in rpc.pb.o

      net::RpcMessage::MergeFrom(net::RpcMessage const&) in rpc.pb.o

      google::protobuf::internal::InternalMetadata::InternalMetadata(google::protobuf::Arena*, bool) in rpc.pb.o

      google::protobuf::internal::EpsCopyInputStream::DoneWithCheck(char const**, int) in rpc.pb.o

      ...

  "google::protobuf::internal::LogMessage::operator<<(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:

      net::ProtobufCodecLite::serializeToBuffer(google::protobuf::Message const&, net::Buffer*) in ProtobufCodecLite.o

  "google::protobuf::internal::VerifyUTF8(google::protobuf::stringpiece_internal::StringPiece, char const*)", referenced from:

      google::protobuf::internal::VerifyUTF8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*, char const*) in rpc.pb.o

  "google::protobuf::internal::WireFormat::InternalSerializeUnknownFieldsToArray(google::protobuf::UnknownFieldSet const&, unsigned char*, google::protobuf::io::EpsCopyOutputStream*)", referenced from:

      net::RpcMessage::_InternalSerialize(unsigned char*, google::protobuf::io::EpsCopyOutputStream*) const in rpc.pb.o

  "google::protobuf::internal::LogFinisher::operator=(google::protobuf::internal::LogMessage&)", referenced from:

      net::ProtobufCodecLite::serializeToBuffer(google::protobuf::Message const&, net::Buffer*) in ProtobufCodecLite.o

      google::protobuf::internal::ToIntSize(unsigned long) in ProtobufCodecLite.o

      ByteSizeConsistencyError(int, int, int) in ProtobufCodecLite.o

      net::RpcMessage::SharedDtor() in rpc.pb.o

      net::RpcMessage::MergeFrom(net::RpcMessage const&) in rpc.pb.o

      google::protobuf::internal::InternalMetadata::InternalMetadata(google::protobuf::Arena*, bool) in rpc.pb.o

      google::protobuf::internal::EpsCopyInputStream::DoneWithCheck(char const**, int) in rpc.pb.o

      ...

  "google::protobuf::internal::VerifyVersion(int, int, char const*)", referenced from:

      (anonymous namespace)::ProtobufVersionCheck() in ProtobufCodecLite.o

      (anonymous namespace)::ProtobufVersionCheck() in RpcCodec.o

  "google::protobuf::internal::ArenaStringPtr::DestroyNoArenaSlowPath()", referenced from:

      google::protobuf::internal::ArenaStringPtr::DestroyNoArena(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*) in rpc.pb.o

  "google::protobuf::internal::ArenaStringPtr::Set(google::protobuf::internal::ArenaStringPtr::EmptyDefault, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&, google::protobuf::Arena*)", referenced from:

      void google::protobuf::internal::ArenaStringPtr::SetBytes<google::protobuf::internal::ArenaStringPtr::EmptyDefault, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(google::protobuf::internal::ArenaStringPtr::EmptyDefault, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&, google::protobuf::Arena*) in RpcChannel.o

  "google::protobuf::internal::ArenaStringPtr::Set(google::protobuf::internal::ArenaStringPtr::EmptyDefault, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, google::protobuf::Arena*)", referenced from:

      net::RpcChannel::CallMethod(google::protobuf::MethodDescriptor const*, google::protobuf::RpcController*, google::protobuf::Message const*, google::protobuf::Message*, google::protobuf::Closure*) in RpcChannel.o

      net::RpcMessage::RpcMessage(net::RpcMessage const&) in rpc.pb.o

      net::RpcMessage::MergeFrom(net::RpcMessage const&) in rpc.pb.o

  "google::protobuf::internal::ArenaStringPtr::Mutable(google::protobuf::internal::ArenaStringPtr::EmptyDefault, google::protobuf::Arena*)", referenced from:

      net::RpcMessage::_internal_mutable_service() in rpc.pb.o

      net::RpcMessage::_internal_mutable_method() in rpc.pb.o

      net::RpcMessage::_internal_mutable_request() in rpc.pb.o

      net::RpcMessage::_internal_mutable_response() in rpc.pb.o

  "google::protobuf::internal::WireFormatLite::VerifyUtf8String(char const*, int, google::protobuf::internal::WireFormatLite::Operation, char const*)", referenced from:

      google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(char const*, int, google::protobuf::internal::WireFormat::Operation, char const*) in rpc.pb.o

  "google::protobuf::internal::ReadTagFallback(char const*, unsigned int)", referenced from:

      google::protobuf::internal::ReadTag(char const*, unsigned int*, unsigned int) in rpc.pb.o

  "google::protobuf::internal::ThreadSafeArena::~ThreadSafeArena()", referenced from:

      google::protobuf::Arena::~Arena() in rpc.pb.o

  "google::protobuf::internal::AssignDescriptors(google::protobuf::internal::DescriptorTable const* (*)(), std::__1::once_flag*, google::protobuf::Metadata const&)", referenced from:

      net::RpcMessage::GetMetadata() const in rpc.pb.o

  "google::protobuf::internal::AssignDescriptors(google::protobuf::internal::DescriptorTable const*, bool)", referenced from:

      net::MessageType_descriptor() in rpc.pb.o

      net::ErrorCode_descriptor() in rpc.pb.o

  "google::protobuf::internal::UnknownFieldParse(unsigned long long, google::protobuf::UnknownFieldSet*, char const*, google::protobuf::internal::ParseContext*)", referenced from:

      net::RpcMessage::_InternalParse(char const*, google::protobuf::internal::ParseContext*) in rpc.pb.o

  "google::protobuf::internal::VarintParseSlow64(char const*, unsigned int)", referenced from:

      google::protobuf::internal::VarintParseSlow(char const*, unsigned int, unsigned long long*) in rpc.pb.o

  "google::protobuf::internal::EpsCopyInputStream::DoneFallback(int, int)", referenced from:

      google::protobuf::internal::EpsCopyInputStream::DoneWithCheck(char const**, int) in rpc.pb.o

  "google::protobuf::internal::AddDescriptorsRunner::AddDescriptorsRunner(google::protobuf::internal::DescriptorTable const*)", referenced from:

      ___cxx_global_var_init.1 in rpc.pb.o

  "google::protobuf::internal::InlineGreedyStringParser(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, char const*, google::protobuf::internal::ParseContext*)", referenced from:

      net::RpcMessage::_InternalParse(char const*, google::protobuf::internal::ParseContext*) in rpc.pb.o

  "google::protobuf::internal::fixed_address_empty_string", referenced from:

      google::protobuf::internal::GetEmptyStringAlreadyInited() in rpc.pb.o

      net::_RpcMessage_default_instance_ in rpc.pb.o

  "google::protobuf::MessageLite::SerializeAsString() const", referenced from:

      net::RpcChannel::CallMethod(google::protobuf::MethodDescriptor const*, google::protobuf::RpcController*, google::protobuf::Message const*, google::protobuf::Message*, google::protobuf::Closure*) in RpcChannel.o

      net::RpcChannel::doneCallback(google::protobuf::Message*, long long) in RpcChannel.o

  "google::protobuf::MessageLite::SerializeWithCachedSizesToArray(unsigned char*) const", referenced from:

      net::ProtobufCodecLite::serializeToBuffer(google::protobuf::Message const&, net::Buffer*) in ProtobufCodecLite.o

  "google::protobuf::ServiceDescriptor::FindMethodByName(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from:

      net::RpcChannel::onRpcMessage(std::__1::shared_ptr<net::TcpConnection> const&, std::__1::shared_ptr<net::RpcMessage> const&, Timestamp) in RpcChannel.o

  "google::protobuf::Message::GetTypeName() const", referenced from:

      vtable for net::RpcMessage in rpc.pb.o

  "google::protobuf::Message::SpaceUsedLong() const", referenced from:

      vtable for net::RpcMessage in rpc.pb.o

  "google::protobuf::Message::InitializationErrorString() const", referenced from:

      vtable for net::RpcMessage in rpc.pb.o

  "google::protobuf::Message::MaybeComputeUnknownFieldsSize(unsigned long, google::protobuf::internal::CachedSize*) const", referenced from:

      net::RpcMessage::ByteSizeLong() const in rpc.pb.o

  "typeinfo for google::protobuf::RpcChannel", referenced from:

      typeinfo for net::RpcChannel in RpcChannel.o

  "typeinfo for google::protobuf::Closure", referenced from:

      typeinfo for google::protobuf::internal::MethodClosure2<net::RpcChannel, google::protobuf::Message*, long long> in RpcChannel.o

  "typeinfo for google::protobuf::Message", referenced from:

      std::__1::shared_ptr<net::RpcMessage> down_pointer_cast<net::RpcMessage, google::protobuf::Message>(std::__1::shared_ptr<google::protobuf::Message> const&) in RpcChannel.o

      typeinfo for net::RpcMessage in rpc.pb.o

  "vtable for google::protobuf::RpcChannel", referenced from:

      google::protobuf::RpcChannel::RpcChannel() in RpcChannel.o

  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

  "vtable for google::protobuf::MessageLite", referenced from:

      google::protobuf::MessageLite::MessageLite(google::protobuf::Arena*, bool) in rpc.pb.o

      google::protobuf::MessageLite::~MessageLite() in rpc.pb.o

      google::protobuf::MessageLite::MessageLite() in rpc.pb.o

  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

  "vtable for google::protobuf::Closure", referenced from:

      google::protobuf::Closure::Closure() in RpcChannel.o

  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

  "vtable for google::protobuf::Message", referenced from:

      google::protobuf::Message::Message(google::protobuf::Arena*, bool) in rpc.pb.o

      google::protobuf::Message::Message() in rpc.pb.o

  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

查看错误信息,可以知道找不到 protobuf 相关的引用库所致,

解决方式,引入头文件目录和库文件目录;

5、使用port安装的程序,头文件安装位置路径/opt/local/include,生成库文件目录/opt/local/lib,在项目工程中添加指定头文件目录和库目录搜索位置,

如图,头文件目录和库目录,

​​​​​​​

 6、快捷键commad+shift+g调出库目录,找到需要的库文件,拖拽到,"Build Phases"->"Link Binary With Libraries"项下,如图,

小结,到此已完成了 C++ 中对 protobuf 的一次使用,主要注意一下使用过程中的碰到的问题,及解决方式。

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

大道之道

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值