March 04, 2011 at 08:28 Tags C & C++

The Google Protocol Buffers library has very good documentation. However, as I was following their C++ tutorial, it wasn't clear how to actually build and make the examples run on Windows, since the tutorial itself only specifies how to translate the .proto file it presents into auto-generated C++ code.

Turns out this isn't particularly difficult, and is actually documented once you dig into the README files in the protobuf installation, but I decided to write down all the steps here for myself, in order to spend less time on it when I need to do it again. This guide refers to version 2.4.0 of protobuf, using Microsoft Visual C++ (MSVC) 2008 , although other versions shouldn't be much different.

The key point to understand here is that protobuf consists of two parts:

The protobuf compiler ( protoc ), that takes a .proto file and produces a .pb.h & .pb.cc pair from it. How to do this is explained in the tutorial. The protobuf runtime, which consists of a set of header files and source files compiled into a static library . To actually use the auto-generated protobuf code, you must link with this runtime.

First, download and unzip the protobuf source distribution for Windows . From now on all directory and file references are relative to the directory you extracted protobuf into. The instructions I was following are roughly those in vsprojects/readme.txt .

Open vsprojects/protobuf.sln in MSVC (you may need to convert the solution - MSVC will prompt you). Set the build type to Release and build the project. It takes a couple of minutes.

Enter the Release directory (this and other steps are better done from the command-line shell) and run tests.exe to see everything built correctly.

Now, we're interested in just two files:

protoc.exe : the protobuf compiler.

: the protobuf compiler. libprotobuf.lib : the static library with the code of the protobuf runtime. Note that it's huge (18MB on my machine), but don't let it scare you - the linker will only take the code it needs from it when linking to your application and the end result isn't too bad.

We also need the header files. For that, run vsprojects/extract_includes.bat . It should create a directory named include (in vsprojects , if this is where you ran the batch file from). These are the public headers of protobuf - the ones you should point your compiler to when trying to compile & link with the protobuf runtime.

Now we're ready to actually build the examples. Go to examples and run:

> ..\vsprojects\Release\protoc --cpp_out=. addressbook.proto >

The files addressbook.pb.h and addressbook.pb.cc were (hopefully) created. The examples directory has two files with main functions to demonstrate usage of this auto-generated code. I'll use add_person.cc for this guide.

Finally, create a new MSVC solution & project for the example, and add add_person.cc , addressbook.pb.h and addressbook.pb.cc as source files. Set the build to Release. In the project's properties:

Add an additional include path, pointing to the full path of vsprojects/include

Add an additional library directory, pointing to the full path of vsprojects/Release

Add libprotobuf.lib as an additional dependency

You should now be able to successfully build and run the example project.