使用proto教程时链接错误

Linking errors when using proto tutorial

本文关键字:链接 错误 教程 proto 使用      更新时间:2023-10-16

我在ubuntu中使用c++,使用apatana作为IDE。

我编译了addressbook.proto文件如下:

protoc -I=/home/workspace/rtb/src/ --cpp_out=/home/workspace/rtb/src/ /home/workspace/rtb/src/addressbook.proto

然后,我将输出头文件包含在我的c++代码中,如下所示:

#include "addressbook.pb.h"

应该是这样吧?

当我建造。。。我得到一个错误列表,这个列表太长了。下面是最后几行。我错过了什么?如何在c++中使用proto缓冲区?

./src/addressbook.pb.o:(.rodata._ZTVN8tutorial11AddressBookE[vtable for tutorial::AddressBook]+0x40): undefined reference to `google::protobuf::Message::InitializationErrorString() const'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial11AddressBookE[vtable for tutorial::AddressBook]+0x48): undefined reference to `google::protobuf::Message::CheckTypeAndMergeFrom(google::protobuf::MessageLite const&)'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial11AddressBookE[vtable for tutorial::AddressBook]+0x88): undefined reference to `google::protobuf::Message::DiscardUnknownFields()'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial11AddressBookE[vtable for tutorial::AddressBook]+0x90): undefined reference to `google::protobuf::Message::SpaceUsed() const'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial6PersonE[vtable for tutorial::Person]+0x20): undefined reference to `google::protobuf::Message::GetTypeName() const'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial6PersonE[vtable for tutorial::Person]+0x40): undefined reference to `google::protobuf::Message::InitializationErrorString() const'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial6PersonE[vtable for tutorial::Person]+0x48): undefined reference to `google::protobuf::Message::CheckTypeAndMergeFrom(google::protobuf::MessageLite const&)'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial6PersonE[vtable for tutorial::Person]+0x88): undefined reference to `google::protobuf::Message::DiscardUnknownFields()'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial6PersonE[vtable for tutorial::Person]+0x90): undefined reference to `google::protobuf::Message::SpaceUsed() const'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial18Person_PhoneNumberE[vtable for tutorial::Person_PhoneNumber]+0x20): undefined reference to `google::protobuf::Message::GetTypeName() const'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial18Person_PhoneNumberE[vtable for tutorial::Person_PhoneNumber]+0x40): undefined reference to `google::protobuf::Message::InitializationErrorString() const'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial18Person_PhoneNumberE[vtable for tutorial::Person_PhoneNumber]+0x48): undefined reference to `google::protobuf::Message::CheckTypeAndMergeFrom(google::protobuf::MessageLite const&)'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial18Person_PhoneNumberE[vtable for tutorial::Person_PhoneNumber]+0x88): undefined reference to `google::protobuf::Message::DiscardUnknownFields()'
./src/addressbook.pb.o:(.rodata._ZTVN8tutorial18Person_PhoneNumberE[vtable for tutorial::Person_PhoneNumber]+0x90): undefined reference to `google::protobuf::Message::SpaceUsed() const'
./src/addressbook.pb.o:(.rodata._ZTIN8tutorial11AddressBookE[typeinfo for tutorial::AddressBook]+0x10): undefined reference to `typeinfo for google::protobuf::Message'
./src/addressbook.pb.o:(.rodata._ZTIN8tutorial6PersonE[typeinfo for tutorial::Person]+0x10): undefined reference to `typeinfo for google::protobuf::Message'
./src/addressbook.pb.o:(.rodata._ZTIN8tutorial18Person_PhoneNumberE[typeinfo for tutorial::Person_PhoneNumber]+0x10): undefined reference to `typeinfo for google::protobuf::Message'
collect2: ld returned 1 exit status
make: *** [rtb] Error 1

您必须将代码链接到协议缓冲区库。很可能,您需要的标志是
-l protobuf