超级计算机囧囧囧's Archivers

From admin on 2011-03-17 18:40:10

Building protobuf examples on Windows with MSVC

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 [1], although other versions shouldn’t be much different.

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

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

  2. The protobuf runtime, which consists of a set of header files and source files compiled into a static library [2]. To actually use the auto-generated protobuf code, you must link with this runtime.


First, download and unzip the protobuf source distribution for Windows [3]. 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:

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:

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



[1]
It works with both the full and express editions of MSVC 2008.






[2]
It’s possible to compile protobuf to a DLL as well, but static linking is the recommended approach, so I’m using it here.






[3]
Note that protoc can be downloaded separately as a pre-compiled binary for Windows. Since we have to build the other parts of protobuf anyway, I don’t see much merit in this separate download.


查看完整版本: Building protobuf examples on Windows with MSVC

Tags: protobuf


©超级计算机囧囧囧