Skip to content

Instantly share code, notes, and snippets.

@madelinegannon
Last active April 4, 2024 12:52
Show Gist options
  • Save madelinegannon/393fe7b1a24664abd0976467162f5f14 to your computer and use it in GitHub Desktop.
Save madelinegannon/393fe7b1a24664abd0976467162f5f14 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

@billgale
Copy link

billgale commented Sep 14, 2021

The latest release lacks Google's third party unit tests. These are recommended by Google for installations to confirm you have a working protobuf environment (with a warning if any fail all bets are off for anything protobuf).

Here are the steps I followed to add them, and build and test everything. Note the steps are slightly different from the above, and more aligned to the protobuf/src/README.md for C++ installation.

If unit tests fail, or you need to try again, do 'make clean/make uninstall' and start at Configure.

Clone protobuf repo
$ git clone git@github.com:protocolbuffers/protobuf.git /somepath/github-google_protobuf
$ cd /somepath/github-google_protobuf
$ git submodule update --init --recursive

Download latest release, eg:
https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protobuf-cpp-3.17.3.zip

Install in /usr/local/bin
$ sudo mv ~/Downloads/protobuf-3.7.13 /usr/local/bin/.

Copy thirdparty tests from local repo
$ cd /usr/local/bin/protobuf-3.7.13/third_party
$ cp -rp /somepath/github-google_protobuf/third_party/* .
$ cd ..

Configure
$ ./autogen.sh
$ ./configure

Build (confirm all tests pass)
$ make
$ make check

Install
$ make install

Confirm
$ protoc --version
libprotoc 3.17.3

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