Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save atifkarim/f171f282e692fb85639d25780ac6caad to your computer and use it in GitHub Desktop.
Save atifkarim/f171f282e692fb85639d25780ac6caad to your computer and use it in GitHub Desktop.
Google Protobuf Tutorial for Mac and c++

How to Install and Run Google's Protocol Buffer Basics: C++ Tutorial on Mac

Tested on: macOS High Sierra v10.13.3

1. Install Google Protobuf

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:

  1. First, unzip the file.

  2. 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

2. Build the Example Tutorial files

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

3. Run the Executables

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment