How to Install and Run Google's Protocol Buffer Basics: C++ Tutorial on Mac
Tested on: macOS High Sierra v10.13.3
Google's official README is here. Follow their instructions to install the special dependencies that mac needs.
The steps I share below are a combination of this gist, this gist, and this answer. The main difference is that I started by manually downloading the latest cpp release from google's release repo. If you start from there, here's what to do next:
-
First, unzip the file.
-
Then, Open Terminal and run the following:
Move the downloaded folder into /usr/local/bin
$ sudo mv ~/Downloads/protobuf-3.6.1 /usr/local/bin
Go into that directory
$ cd /usr/local/bin/protobuf-3.6.1
Configure how cmake is going to build protoc
$ ./configure CC=clang CXX="clang++ -std=c++11 -stdlib=libc++" CXXFLAGS="-O3" --disable-shared
Make and Install
$ make
$ sudo make install
If installed properly, you should see the library version when you run the following:
$ protoc --version
libprotoc 3.6.1
All together it looks like this:
$ sudo mv ~/Downloads/protobuf-3.6.1 /usr/local/bin
$ cd /usr/local/bin/protobuf-3.6.1
$ ./configure CC=clang CXX="clang++ -std=c++11 -stdlib=libc++" CXXFLAGS="-O3" --disable-shared
$ make
$ sudo make install
$ protoc --version
libprotoc 3.6.1
How to run Google's Protocol Buffer Basics: C++ Tutorial
Build the addressbook proto:
$ cd examples/
$ protoc --cpp_out=. addressbook.proto
You should see two new files , addressbook.pb.h
and addressbook.pb.cc
, in the examples/
directory.
Build the add_people
and list_people
executables from the command line:
$ clang++ -std=c++11 -stdlib=libc++ add_people.cc addressbook.pb.cc -L/usr/local/lib -lprotobuf -o add_people_cpp
$ clang++ -std=c++11 -stdlib=libc++ list_people.cc addressbook.pb.cc -L/usr/local/lib -lprotobuf -o list_people_cpp
Once everthing is linked and built properly, you can run the apps:
Start the add_people
app:
$ ./add_people_cpp addressbook.data
Follow the command prompts
Start the list_people
app:
$ ./list_people_cpp addressbook.data
You should see all your entries added from add_people_cpp
Thanks for your great help! Stuck on C++ part for quite a while.