Apache Thrift for C++ on Visual Studio 2015

Today we are going to see how to build Apache Thrift for C++ on Visual Studio 2015. Then for demonstration, we’ll also build and run the C++ tutorial.

Disclaimer: this tutorial builds hugely on the one given by Adil Bukhari Configuring Apache Thrift for Visual Studio 2012. The reason I create a new one is that I followed his steps but stumbled upon a few problems preventing me from continuing it. Therefore, I find it quite helpful –for future learners– to complement that tutorial with the solutions to these problems.

Testing environment

  • Windows 10 64bit.
  • Microsoft Visual Studio 2015 (also tested with Visual Studio 2013).
  • Apache Thrift 0.9.2.
  • Boost 1.59.0.
  • Libevent 2.0.22.
  • OpenSSL 1.0.2d.
  • Summer time 🙂

Requirements

  1. Download Apache Thrift and Thrift compiler for Windows from the download page here.
  2. Download and build Boost libraries (also follow Adil’s tutorial here: Configuring C++ Boost Libraries for Visual Studio).
  3. Download libevent library from the official webpage.
  4. Download OpenSSL for Windows (when you are on the OpenSSL binaries page, follow the link they suggest under OpenSSL for Windows — Works with MSVC++), and then install it.

Building Apache Thrift libraries

  1. Open the Developer Visual Studio 2015 Command Prompt that you find via
    Start > All Programs > Visual Studio 2015 > Developer Command Prompt for VS2015
  2. Therein, navigate to the unzipped libevent directory (where sit sub-directories such as “compat”, “include”, and “test”).
  3. Run the command  nmake -f Makefile.nmake, which will build libevent.
  4. Extract Apache Thrift tar file and navigate to {thrift_install_dir}\lib\cpp, then open thrift.sln project with Visual Studio 2015 (you’ll probably be prompted to upgrade the project, just accept it kindly!). The solution contains two projects: libthrift and libthriftnb.
  5. Right-click on libthrift project and select Properties, then go to C/C++ > General and under Additional Include Directories enter the following line:  {boost_install_dir};{boost_install_dir}\boost_1_56_0\boost;{openssl_install_dir}\include , then to Librarian > General and under Additional Library Directories enter the following line: {openss_install_dir}\lib.
  6. Right-click now on libthriftnb project and select Properties, then go to C/C++->General and under Additional Include Directories enter the following line:  {boost_install_dir};{libevent_install_dir};{libevent_install_dir}\include;{libevent_install_dir}\WIN32-Code;.
  7. Inside libthrift project content, expand concurrency folder and Remove BoostThreadFactory.cpp file. This file causes compilation issues and must be removed in order to continue.
  8. Select type of build: Debug or Release, and Build the two projects. Upon a success, you will see libthrift.lib and libthriftnb.lib files inside Debug or Release directory respectively.

Building Apache Thrift Server

  1. Download the tutorial.thrift file (from here) and shared.thrift file (from here) –save them to the location where Thrift compiler for Window (thrift_x.y.z.exe) is located.
  2. Run these the following two commands to generate C++ code for both of these service definition files:


    This will generate a directory called gen-cpp in the same previous location.
  3. Create a new Visual Studio project and drug-and-drop the content of the gen-cpp folder into it: *.h files under Header Files and *.ccp files under Sources Files, so it gives this structure: 
    All of the gen-cpp files imported into a VC++ project.
  4. Right-click on your project and select Properties, then go to C/C++ > All Options, and under Additional Include Directories enter the following line:  {boost_install_dir};{thrift_install_dir}\lib\cpp\src\thrift\windows;{thrift_install_dir}\lib\cpp\src;, then to Linker > All Options, and under Additional Include Dependencies enter the following line:  libboost_thread-vc140-mt-gd-1_59.lib;libboost_chrono-vc140-mt-gd-1_59.lib;libthrift.lib;%(AdditionalDependencies)Beware:You’ll modify these manually so be careful to the version numbers. In my case I downloaded Boost for Visual Studio 2015 (boost_1_59_0-msvc-14.0-64.exe), hence the presence of cv14, and 1_59. Change these two values following your Boost version you downloaded.

Note: The above two libraries are located under {boost_install_dir}/stage/lib….,…then to Linker > All Options and under Additional Library Directories enter the following line:  {boost_install_dir}\stage\lib;{thrift_install_dir}\lib\cpp\Debug

Note: The second item in the path could end with \Release, if you used the Release configuration instead of Debug while compiling Apache Thrift (I used Debug for this tutorial).

  1. Remove skeleton files (*.skeleton.cpp) from the project.
  2. Build the project. If it succeeds than you can start the server.

    Building Apache Thrift Client

  1. Create a new Visual Studio project and drug-and-drop the content of the gen-cpp folder into it: *.h files under Header Files and *.ccp files under Sources Files,
  2. Download the CppClient.cpp file (from here) –save it under Sources Files.
  3. Remove skeleton files (*.skeleton.cpp) from the project.
  4. Right-click on the project and select Properties, then go to C/C++ > All Options and under Additional Include Directories enter the following line:  {boost_install_dir};{thrift_install_dir}\lib\cpp\src\thrift\windows;{thrift_install_dir}\lib\cpp\src;, then to Linker > All Options and under Additional Library Directories enter the following line:  libboost_thread-vc140-mt-gd-1_59.lib;libboost_chrono-vc140-mt-gd-1_59.lib;libthrift.lib;%(AdditionalDependencies)

    Beware & note: [see point 4 of the previous section (Build Apache Thrift Server)].

    …then to Linker > All Options and under Additional Library Directories enter the following line:  {boost_install_dir}\stage\lib;{thrift_install_dir}\lib\cpp\Debug

    Note: [see point 4 of the previous section (Build Apache Thrift Server)].

  5. Build the project. If it succeeds than you can start the client (Ensure that your Server is already running).

Hula hoop! enjoy watching client and server running business 🙂

NB: as anticipated in the introduction, the above steps have been also tested under Visual Studio 2013 with slight changes that you can guess yourselves, I guess 🙂

||||| 3 I Like It! |||||

Comments

comments

3 thoughts on “Apache Thrift for C++ on Visual Studio 2015

  1. can you get the named pipe sample to work from the contrib transport folder? i cant figure it out 🙁

    Reply

  2. At the current state this is a buggy tutorial since there is a lot of usual floss in used libraries (like thrift vc project lists file for thrift that is removed, openssl calls function that is removed…) but that aside your tutorial has been a good general direction help. Thank you.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *