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
Tested on macOS Big Sur 11.5.2
Works, even though could not resolve protobuf config file, as I installed protobuf using brew so there was no cmake install. This did not appear to impact protobuf compilation or the program.
Corrections
You need to change the add program name in the README, in my examples folder the file is add_person.cc.
So the compile becomes:
$ clang++ -std=c++11 -stdlib=libc++ add_person.cc addressbook.pb.cc -L/usr/local/lib -lprotobuf -o add_person_cpp
And the executable is invoked as:
$ ./add_person_cpp addressbook.data