c++ - 如何编译Google Protobuf命令行界面编译

标签 c++ protocol-buffers

我正在尝试对 protobuf 二进制文件进行原始解码。我从源代码安装了 google protobuf 库 https://github.com/google/protobuf 我能够使用命令行使用命令 protoc --decode_raw <encodedfile> 解码原始的 protobuf 二进制文件.我希望能够使用 c++ 库以编程方式执行此操作。类似于文档中的以下示例。

https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.compiler.command_line_interface

但是尝试编译一段简单的代码是行不通的。

#include <iostream>
#include <fstream>
#include <string>
#include <google/protobuf/compiler/command_line_interface.h>
using namespace google::protobuf::compiler;
using namespace std;

int main(int argc, char* argv[]) {


    google::protobuf::compiler::CommandLineInterface cli_;
    cerr << "Compiled And Run" << endl;

}

编译命令

c++  my_program.cc  -o my_program -pthread -I/usr/local/include  -pthread -L/usr/local/lib -lprotobuf -lpthread

我看到以下错误

my_program.cc:(.text+0x24): undefined reference to `google::protobuf::compiler::CommandLineInterface::CommandLineInterface()'
my_program.cc:(.text+0x4f): undefined reference to `google::protobuf::compiler::CommandLineInterface::~CommandLineInterface()'
my_program.cc:(.text+0x70): undefined reference to `google::protobuf::compiler::CommandLineInterface::~CommandLineInterface()'

感谢对此的任何帮助。

最佳答案

Protobuf 编译器位于不同的库 libprotoc 中。你需要链接到它

c++  my_program.cc  -o my_program -pthread -I/usr/local/include  -pthread -L/usr/local/lib -lprotoc -lprotobuf -lpthread

注意-lprotoc需要出现在-lprotobuf之前,因为libprotoc使用了libprotobuf的函数。

关于c++ - 如何编译Google Protobuf命令行界面编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45301771/

相关文章:

c++ - 在 linux 中播放 wav 文件

c++ - 我在 g++ 中的(简单)代码链接,在 clang 中没有链接

c++ - 快速、模板化、C++ 八叉树实现

c++ - C和C++的指针区别

c# - Protobuf 解码任意消息。 Protobuf 消息多态性

c++ - 为什么链表中的数据在嵌套函数中发生更改/损坏?

java - j2objc/usr/local/lib//libprotobuf.a(strutil.o) ld : 55 duplicate symbols for architecture x86_64

go - 在Golang中解码Protobuf(proto2)后如何设置可选参数?

c# - 在 c# visual studio 中编译 Protocol Buffer 3 时间戳类型?

python - 加载 SavedModel 比加载 tf.train.Saver 检查点慢很多