Published on

Using Apache Thrift 2: Environment Setup with Visual C++

Authors

This time I will write about building the Thrift compiler and the C++ libraries. The environment is Windows XP 32bit with Visual C++ 2010 Express.

  1. Build the Thrift compiler
  2. Build libevent (optional: required if you want to use Nonblocking Server)
  3. Build libthrift

1. Build the Thrift compiler

The official page currently publishes a stable compiler release. However, 0.80 was not fully compatible with Windows, and I had a very hard time because runtime errors appeared at the final stage.

These issues had been addressed toward 0.90, so one option would be to review the SVN log and manually incorporate the relevant fixes, but that is tedious, so I preferred to use the latest version.

The version difference between the compiler and the library may not matter that much, but I set up the environment using the same version for both.

The page below explains how to set up the environment on Windows, but several parts do not work as-is.

http://wiki.apache.org/thrift/ThriftInstallationWin32 First, set up the build environment for the Thrift compiler.

1.1 Install MinGW

Use MinGW to build the compiler.

Download the latest MinGW installer from here.

Run the downloaded mingw-get-inst-*.exe and install it. Leave the default selections as they are, but for the environment components to install, check the following items.

  • C Compiler
  • C++ Compiler
  • MSYS Basic System
  • MinGW Developer Toolkit

1.2 Install pkg-config

If pkg-config is missing, configure fails. Specifically, you get an error like the following. Also, if you see a message such as PKG_PROG_PKG_CONFIG not found near the beginning of configure, you are in trouble.

/configure: line 16989: syntax error near unexpected token QT,' ./configure: line 16989: PKG_CHECK_MODULES(QT, QtCore >= 4.3, QtNetwork >= 4.3, have_qt=yes, have_qt=no)'

So, install pkg-config. Fortunately, the required tools are available from the GTK project. From here, under [Download] -> [Windows (32-bit)], download:

  • pkg-config: Tool
  • GLib: Run-time
  • gettext-runtime: Run-time

Extract those three archives, then copy the extracted folders directly into C:\MinGW and overwrite the existing files. You also need to obtain the pkg macro files from the pkg-config source code.

From the GTK page above or from the [[/releases]] link in the middle of this page, download the source for the same version of pkg that you downloaded above. After extracting it, you should find a file named pkg.m4. Copy that file into MinGW\share\aclocal and MinGW\share\aclocal-1.11 (the newest version directory).

Now fetch the latest Thrift project.

$ svn co http://svn.apache.org/repos/asf/thrift/trunk/ thrift

Open thrift/compiler/cpp/Makefile.am in an editor and modify the following line.

CXX_FLAGS= -DMINGW -static

Then open MSYS, move into the downloaded thrift folder, and run:

./bootstrap.sh./bootstrap.sh export CXXFLAGS=”-DPTHREAD_MUTEX_RECURSIVE_NP=PTHREAD_MUTEX_RECURSIVE” ./configure./configure cd compiler/cpp $ make

If everything worked correctly up to this point, thrift.exe should have been created at the top of the folder.

2. Build libevent (optional: required if you want to use Nonblocking Server)

If you want to use NonblockingServer, you need libevent. Download the stable version from the official site. After extracting it, open the Visual Studio command prompt.

cd/path/to/libeventcd /path/to/libevent mv Makefile.nmake Makefile $ nmake

This creates the following three libraries:

  • libevent.lib
  • libevent_core.lib
  • libevent_extras.lib

You also need to collect the required include files. In the extracted folder's include directory, copy the contents of WIN32-Code, as well as evdns.h, event.h, evhttp.h, evrpc.h, and evutil.h from the extracted root. That completes the libevent library.

3. Build libthrift

Open thrift/lib/cpp/thrift.sln. It cannot be opened by double-clicking, so start Visual C++ first and drag and drop it in. A warning appears, but ignore it. Once it is open, add the Boost include and lib directories from the libthrift project properties. When you build it, libthrift.lib is created under the Release folder.

If you want to use NonblockingServer:

You also need to build libthriftnb. In the project properties, add the Boost include and lib directories and the libevent include and lib directories. Building it creates libthriftnb.lib under the Release folder. That completes the library build. Next time I will describe how to actually use it.