【问题标题】:Run protobuf given example on eclipse (C++)在 Eclipse (C++) 上运行给定示例的 protobuf
【发布时间】:2019-06-14 18:54:31
【问题描述】:

我正在尝试使用和理解协议缓冲区。我已经安装了库并尝试运行该示例。但我看不出哪里出错了。

下面我将描述所有步骤: 1.按照说明安装protobuf编译器https://github.com/protocolbuffers/protobuf/tree/master/src

2. 按照说明安装protobuf插件到eclipsehttps://github.com/google/protobuf-dt (不确定是否需要)。

3. 打开 protobuf/examples/ 目录。在eclipse中运行add_person.cc 编译器产生以下错误。我看到这是一个链接错误。但我不确定我哪里出错了。

make all 
Building file: ../src/add_person.cc
Invoking: GCC C++ Compiler
g++ -std=c++11 -I/home/rohan/Downloads/Utilities/protobuf/examples -O0 -g3 -Wall -c -fmessage-length=0 -pthread -MMD -MP -MF"src/add_person.d" -o "src/add_person.o" "../src/add_person.cc"
Finished building: ../src/add_person.cc

Building target: add_person
Invoking: GCC C++ Linker
g++ -L/usr/lib64 -pthread -o "add_person"  ./src/add_person.o   -lprotobuf
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: ./src/add_person.o: in function `main':
/home/rohan/eclipse-workspace/add_person/Debug/../src/add_person.cc:81: undefined reference to `tutorial::AddressBook::AddressBook()'
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: /home/rohan/eclipse-workspace/add_person/Debug/../src/add_person.cc:81: undefined reference to `tutorial::AddressBook::~AddressBook()'
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: /home/rohan/eclipse-workspace/add_person/Debug/../src/add_person.cc:81: undefined reference to `tutorial::AddressBook::~AddressBook()'
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: ./src/add_person.o: in function `google::protobuf::internal::GenericTypeHandler<tutorial::Person_PhoneNumber>::New(google::protobuf::Arena*)':
/usr/local/include/google/protobuf/repeated_field.h:656: undefined reference to `tutorial::Person_PhoneNumber* google::protobuf::Arena::CreateMaybeMessage<tutorial::Person_PhoneNumber>(google::protobuf::Arena*)'
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: ./src/add_person.o: in function `google::protobuf::internal::GenericTypeHandler<tutorial::Person>::New(google::protobuf::Arena*)':
/usr/local/include/google/protobuf/repeated_field.h:656: undefined reference to `tutorial::Person* google::protobuf::Arena::CreateMaybeMessage<tutorial::Person>(google::protobuf::Arena*)'
collect2: error: ld returned 1 exit status
make: *** [makefile:47: add_person] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

【问题讨论】:

  • 你编译过protobuf吗?
  • 在您的示例中有一个名为“addressbook.proto”的文件。您应该通过 protobuf 编译器运行它,该编译器将生成一些 C++ 源文件。您将这些文件添加到您的项目中,它们将解决缺少的依赖项。我不知道你是如何在日食的情况下做到这一点的。你总是可以只使用命令行。
  • @john 是的,我确实编译过。我添加了那些。 addressbook.pb.ccaddressbook.pb.h 这些是从 proto 文件生成的文件。并包含包含-I/home/rohan/Downloads/Utilities/protobuf/examples。显然我可以使用命令行,但我已经用 eclipse 启动了它,这是我以前没有遇到过的一些问题,所以我很想解决这个问题:)
  • 从上面的命令行中没有证据表明您已经编译了 addressbook.pb.cc。 g++ -std=c++11 -I/home/rohan/Downloads/Utilities/protobuf/examples -O0 -g3 -Wall -c -fmessage-length=0 -pthread -MMD -MP -MF"src/add_person.d" -o "src/add_person.o" "../src/add_person.cc" 我希望在那里看到 addressbook.pb.cc 以及 add_person.cc。
  • 那么,预期的线会是什么样子?我认为这将由-I 命令处理

标签: c++ eclipse eclipse-plugin protocol-buffers


【解决方案1】:

请注意:这可能无法解决其他人的问题。

我在eclipse中运行的示例程序在项目中只有add_person.cc文件。并且包含协议缓冲区类和定义的文件(addressbook.pb.h & addressbook.pb.cc)不在项目中。相反,我将它们添加为-I\path\to\the\directory\,这会产生该错误。

对于这个最小的问题,我只需要移动/复制项目中的addressbook.pb.haddressbook.pb.cc 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    相关资源
    最近更新 更多