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
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 [email protected]: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