Ahmed Shahid
Ahmed Shahid

Reputation: 294

How to compile Google Protobuf command line interface compile

I am trying to raw decode a protobuf binary. I installed the google protobuf library from the source code https://github.com/google/protobuf I am able to use the command line to decode raw a protobuf binary using the command protoc --decode_raw <encodedfile>. I want to be able to do this programmatically using the c++ library. Something similar to the following example in the documentation.

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

However trying to compile a simple piece of code does not work.

#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;

}

Compile command

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

I see the following error

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()'

Appreciate any help with this.

Upvotes: 0

Views: 6649

Answers (1)

nsubiron
nsubiron

Reputation: 923

Protobuf compiler is in a different library, libprotoc. You need to link against it

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

Note that -lprotoc needs to appear before -lprotobuf, because libprotoc uses functions of libprotobuf.

Upvotes: 2

Related Questions