C++ Linux Google Protocol Buffers - having trouble replicating the tutorial

328 views
Skip to first unread message

Jonathan

unread,
Jul 18, 2016, 1:25:41 PM7/18/16
to Protocol Buffers

I'm following the cpp tutorial on google protocol buffers and am trying to run the example code.


I successfully installed Google Protocol Buffers on Ubuntu and compiled the .proto file so I got the pb.h and pb.cc generated files.


ReadAddressBook.cpp is the main c++ file that uses Google Protocol Buffers and I made it very minimal:


#include <iostream>
#include <fstream>
#include <string>
#include "address_book.pb.h"
using namespace std;

int main(){

 GOOGLE_PROTOBUF_VERIFY_VERSION;

 tutorial::AddressBook address_book;
 cout << "Created Object";

return 0;
}

Now using g++ I want to run this. The tutorial never mentioned actually how to compile and link these files beyond generating the pb.cc and ph.h files.


So I wrote the commands:


g++ -l /home/{usr}/local/include -L /home/{usr}/local/lib ReadAddressBook.cpp address_book.pb.cc -lprotobuf -pthread
g++ -c ReadAddressBook.cpp
g++ -o ReadAddressBook ReadAddressBook.o


And got the error messages:



Undefined reference to 'google::protobuf::internal' and ect..


How do I link these libraries?



Adam Cozzette

unread,
Jul 19, 2016, 6:07:36 PM7/19/16
to Jonathan, Protocol Buffers
I think the reason for the error you're getting is that your third command does not provide the compiled code from addressbook.proto or the libprotobuf library. I would recommend going into the examples/ directory and running "make cpp"; that will build and run the example code for you and should work as long as you have successfully done "make install". You may just need to tweak your PATH and PKG_CONFIG_PATH if you installed into a non-standard prefix. On my machine I used "./configure --prefix=$HOME/install" and built the example code like this:

PATH=$HOME/install/bin:$PATH PKG_CONFIG_PATH=$HOME/install/lib/pkgconfig make cpp

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages