Configuring Apache Thrift for Visual Studio 2012

Summary

Yes, it’s true: you can run and compile Apache Thrift servers and clients in Visual Studio 2012! Here’s a set of steps for compiling Apache Thrift under Visual Studio 2012 and then running a C++ Server and Client pair. I’ve also included steps to build a Java client which interacts with the C++ server, to demonstrate Thrift’s cross-language capabilities.

The current version of Thrift was 0.9.1 at the time of writing.

 

Build Apache Thrift libraries for VS2012

  1. Prerequisite: Download and build the Boost libraries for VS2012: see Configuring C++ Boost Libraries for Visual Studio. Note your {boost_install_dir} for later.
  2. Download Apache Thrift AND the Thrift compiler for Windows from http://thrift.apache.org/download
  3. Download libevent, a C++ dependency for the non-blocking Thrift Server: http://libevent.org/
  4. Extract both tar.gz files once downloaded.
  5. Navigate to {thrift_install_dir}\lib\cpp and open thrift.sln with VS2012
  6. If a dialog box appears that asks to upgrade some files to VS2012, click Upgrade
  7. Open the Visual Studio 2012 Command Prompt at Start->All Programs->Microsoft Visual Studio 2012 ->Visual Studio Tools -> Visual Studio Command Prompt (2012)
  8. In the VS2012 CMD Prompt, cd into the unzipped libevent directory. You should see subdirectories like “compat”, “include”, and “test” if it is the correct directory.
  9. Run this command in the CMD prompt to build libevent for VS2012: nmake -f Makefile.nmake
  10. Note your {libevent_install_dir}.
  11. Once the command has finished, go back to VS2012 (thrift.sln), right-click libthrift’s Project Properties, and navigate to C/C++->General.
  12. Enter this information into the Additional Include Directories line:
    {boost_install_dir}\boost_1_56_0;{boost_install_dir}\boost_1_56_0\boost
  13. Right-click libthriftnb’s Project Properties, and navigate to C/C++->General.
  14. Enter this information into the Additional Include Directories line:
    {boost_install_dir}\boost_1_56_0;{libevent_install_dir};{libevent_install_dir}\include;{libevent_install_dir}\WIN32-Code;
  15. Counter-intuitive, but necessary: Remove BoostThreadFactory.cpp from the libthrift project. This file causes compilation issues and must be removed in order to continue.
  16. Trigger a Rebuild of both projects, and note the directory where libthrift.lib and libthriftnb.lib are created. This will depend on your configuration for Debug or Release.

Build your Thrift Server and Client

This section will guide you through the Apache Thrift tutorial for C++ in VS2012, found at http://thrift.apache.org/tutorial/cpp

Building and Running the C++ Thrift server

  1. Download the tutorial.thrift and shared.thrift files from Apache:
  2. Run these two Thrift compiler commands to generate C++ for both of these service definition files:
    • thrift -r --gen cpp tutorial.thrift
    • thrift -r --gen cpp shared.thrift
  3. This will generate a gen-cpp directory in the same directory that thrift.exe was run from.
  4. Copy this gen-cpp folder into a Visual Studio Project, and separate the header and source files into the appropriate folders.

    All of the gen-cpp files imported into a VC++ project.

    All of the gen-cpp files imported into a VC++ project.

  5. Next, we must fix some convenient gotchas to bring the tutorial to life.
  6. Right-click your project in the Solution Explorer and go to Properties, then C/C++->All Options, and set Additional Include Directories to:
    {boost_install_dir};{thrift_install_dir}\lib\cpp\src\thrift\windows;{thrift_install_dir}\lib\cpp\src;
  7. Then, go to Linker->All Options, and set Additional Dependencies to:
    libboost_thread-vc110-mt-gd-1_56.lib;libboost_chrono-vc110-mt-gd-1_56.lib;libthrift.lib;%(AdditionalDependencies)

    Note: the version numbers for these libraries may differ if you’ve built a different version of Boost.

  8. Click on Linker->All Options, and set Additional Library Directories to:
    {boost_install_dir}\stage\lib;{thrift_install_dir}\lib\cpp\Debug

    The second item in the path could end with \Release, if you used the Release configuration instead of Debug while compiling Thrift.

  9. Double-click Calculator_server.skeleton.cpp. We need to adjust the main() method for the Server.
  10. Place this code at the top of the int main(int argc, char **argv) method:
      	WSADATA wsaData = {};
        WORD wVersionRequested = MAKEWORD(2, 2);
        int err = WSAStartup(wVersionRequested, &wsaData);
    
  11. Trigger a rebuild. If all goes well, then your project should compile without a hitch, and then you should be able to Run the project and see your C++ Thrift server starting up!

 

Building and running the C++ Thrift client

  1. Create another VS2012 project for the Thrift C++ client.
  2. Download the CppClient.cpp file and copy its contents to a new Source file in your new Client project:
    https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob;f=tutorial/cpp/CppClient.cpp;hb=HEAD
  3. Copy the gen-cpp folder from the Server application into your Client project, and add all of the files to the project. You need two separate copies of these files, or there will be two int main() functions defined in both your server and client, which is not fun to resolve.
  4. Erase or remove the files Calculator_server.skeleton.cpp and SharedService_server.skeleton.cpp from your project.
  5. Right-click your project in the Solution Explorer and go to Properties, then C/C++->All Options, and set Additional Include Directories to:
    {boost_install_dir};{thrift_install_dir}\lib\cpp\src\thrift\windows;{thrift_install_dir}\lib\cpp\src;
  6. Then, go to Linker->All Options, and set Additional Dependencies to:
    libboost_thread-vc110-mt-gd-1_56.lib;libboost_chrono-vc110-mt-gd-1_56.lib;libthrift.lib;%(AdditionalDependencies)

     Note: the version numbers for these libraries may differ if you’ve built a different version of Boost.

  7. Then, go to Linker->All Options, and set Additional Library Directories to:
    {boost_install_dir}\stage\lib;{thrift_install_dir}\lib\cpp\Debug

    The second item in the path could end with \Release, if you used the Release configuration instead of Debug while compiling Thrift.

  8. Trigger a rebuild. If all is well, it should succeed.
  9. Ensure that your Server is running, and run your Client. Thrift lives!
Eureka! The Thrift server accepts commands and responds to them.

Eureka! The Thrift server accepts commands and responds to them.

Addendum: Building and Running a Java Client for your C++ Service

Let’s explore the cross-language power of Thrift by creating a Java client which can talk to the C++ service.

Create Thrift files, and import them into Eclipse

  1. Create a new Java Project in Eclipse.
  2. Run these two Thrift compiler commands to generate Java code for both of these service definition files:
    • thrift -r --gen java tutorial.thrift
    • thrift -r --gen java shared.thrift
  3. This will create a gen-java folder wherever thrift.exe is located. Import all of these files into your Java Project, and sort them into the correct packages: some files will go into a package named ‘tutorial’ and others will go into a package named ‘shared’.
  4. Create a Java file named JavaClient.java, and paste the contents of the sample JavaClient.java into it
    https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/java/src/JavaClient.java;hb=HEAD
  5. So many red marks! Let’s work on fixing those…

 

Build all Java libraries and compile the client

Thrift has two dependencies in Java: ant and slf4j.

  1. Download the latest Apache Ant binary from here: http://ant.apache.org/bindownload.cgi
  2. Download the latest SLF4J binary from here: http://www.slf4j.org/download.html
  3. Unzip both downloads and note their locations.
  4. We won’t be able to build Ant files until we configure Ant correctly.
    Follow Steps 1-5 here: http://ant.apache.org/manual/install.html#getting
  5. Next, download and unzip this maven-ant-tasks library, which we will need for the Thrift compilation:
    http://maven.apache.org/ant-tasks/download.cgi
  6. Copy the unzipped maven-ant-tasks-2.1.3.jar into {ant_install_directory}\lib
  7. Time to build Thrift’s Java library.
  8. Navigate to {thrift_install_dir}\lib\java
  9. Open a Command Prompt, and type ant
    If you’ve set up Ant and the maven-ant-tasks JAR correctly, you will see BUILD SUCCEEDED
  10. Copy the path for the newly-built Thrift Java library, located at “{thrift_install_dir}\lib\java\build\libthrift-0.9.1.jar”
  11. In Eclipse, right-click your project’s JRE System Library in the project explorer, and choose Build Path->Configure Build Path
  12. Click on the Libraries tab and Add External JAR, then add the Thrift Java library.
  13. Finish up by adding the SLF4J dependency to Eclipse.
  14. In Eclipse, right-click your project’s *JRE System Library in the project explorer, and choose Build Path->Configure Build Path
  15. Click on the Libraries tab and Add External JAR, then add the SLF4J jar, located at “{slf4j_install_directory}\slf4j-api-1.7.7.jar”
  16. Ensure that your Thrift server is up, and run your Java project as a Java Application through Eclipse. It works!
  17. If the program exits with a message like “Please enter ‘simple’ or ‘secure'”, then you are missing a program argument. Right-click your project, click Run->Run Configurations, and click on the Arguments tab. Add the word simple or secure to the Program Arguments dialog box, then click Run.

 

Related Links

  1. The Apache/Facebook developers were very Thrifty with their documentation, and produced a one-page tutorial: http://thrift.apache.org/tutorial/cpp
  2. A blog post with instructions for Visual Studio 2010 can be found here: http://technicaltidbit.blogspot.com/2012/12/apache-thrift-in-windows.html
Advertisements

Configuring C++ Boost Libraries for Visual Studio

Summary

Here is a basic guide to getting C++ Boost libraries to work within Visual Studio.

More information about these handy libraries is available here: http://www.boost.org/

 

Installation

 

Download the Boost libraries

  1.  Determine the version of Visual Studio that you will be using for your project. For example, Visual Studio C++ 2012 is also known as version 11. You can find this out by clicking HELP > About Visual Studio and looking for the “Version ##” line on the screen.
  2. Determine whether you will be building a 32-bit or 64-bit version of the Boost libraries.
  3. Now that you have determined these two pieces of information, navigate to http://sourceforge.net/projects/boost/files/boost-binaries/, click on the folder for the latest version, and download the correct .exe installer for your Visual Studio version and architecture.
  4. Once the download is finished, run the installer as normal and note the installation directory.

Build the libraries for your machine

  1. Open the Developer Command Prompt for VS20xx under Start > Microsoft Visual Studio 20xx > Visual Studio Tools

    vs_dev

    The location of the Developer Command Prompt for VS2012.

  2. Once the Developer Command Prompt is open, type in cd <your Boost install directory>
  3. Next, type these commands to build the Boost libraries for your machine: bootstrap and then b2. This last command will run for about ten minutes, so take a break!
  4. Once b2 has finished, you should see it print two lines noting “compiler include paths” and “linker library paths”. Write these paths down or take a screenshot.
boost

The Boost build has finished, and Boost tells us where to find the new libraries.

Link the libraries through Visual Studio

  1. Open your C++ project in Visual Studio 20xx. Right click its name in the Solution Explorer and go to Properties.
  2. Click on Configuration Properties, then C/C++, and then All Options.
  3. Modify Additional Include Directories: append the “compiler include paths” string to the end of it.
  4. Click on Configuration Properties, then Linker, and then All Options.
  5. Modify Additional Library Directories: append the “linker library paths” string to the end of it.
  6. Click OK to save your changes. Ensure that you are including the specific Boost libraries like this at the top of your source files: #include <boost/foreach.hpp>
  7. Try to Rebuild your project. Hopefully, all of your library issues will now be resolved! Happy coding!

 

Other Resources

Parts of this write-up were figured out through Boost’s Visual Studio page.