How-To: Build libtorrent DLL for Windows
Recently, I have been researching on BitTorrent technology as I am required to write an application with BitTorrent support. In the process of searching, I came across a C++ library that is a good alternative to all the other BitTorrent implementations around. Many people may have used this library. But, if you are new to BitTorrent technology and want to write your own BitTorrent client, and blah… blah… to run under Windows, this post is for you.
Before we can start building the libtorrent, we need the following components. Please download them if you have not.
Once we have got the required components downloaded, we can start building steps by steps.
Note: The following sections are for you if you have downloaded the source and not the binaries.
Building Boost Library
I will only cover on how to build boost library by following simple commands. For more details, please read them at Getting started on Windows.
The steps are as follows.
- Unzip the boost file to e.g., C:\boost.
- Add the environment variable BOOST_ROOT and set its value to the boost folder e.g., C:\boost.
- Run the Visual Studio <version> Command Prompt.
- Then, run the following commands.
cd %BOOST_ROOT% bootstrap bjamThe bootstrap command will create bjam.exe. Once the bootstrap is completed, we run the bjam command to build the boost source.
This will take some time. So, let’s have a cup of coffee while waiting for it.
Building OpenSSL Library
- Unzip the openssl file to e.g., C:\OpenSSL
- Add an environment variable called OpenSSL and set its value to the the boost folder e.g., C:\OpenSSL.
- Install the ActivePerl if you have not.
- Unzip the nasm file and add the folder to the Path environment variable.
- Next, we will build the OpenSSL. Run the Visual Studio <version> Command Prompt with the following commands.
cd C:\OpenSSL perl Configure VC-WIN32 --prefix=%OpenSSL% call ms\do_nasm nmake -f ms\nt.makNote that if you get error C2220: warning treated as error after running nmake -f ms\nt.mak, do the following three steps.
- Open up the file ms\nt.mak.
- Remove /WX at CFLAG options.
- Then run nmake -f ms\nt.mak again.
Building libtorrent DLL
Assuming that the libtorrent file is unzip to C:\libtorrent.
- Create the Win32 Project file (e.g. residing in C:\libtorrent\win32).
- You can choose to either create a DLL or static library project. In this post, I will only show you how to create a DLL project. You can also choose to create a static library project if you want to. At the Win32 Application Wizard, choose
- DLL under application type; and
- Empty project under Additional options.
Note: Steps 3 to 6 involves Project->Properties.
- Add the followings to Additional Include Directories under C/C++->General property page.
$(BOOST_ROOT) $(OpenSSL)\inc32 C:\libtorrent\include C:\libtorrent\zlib C:\libtorrent\include\libtorrent - Add the followings to Preprocessor Definitions under C/C++->Preprocessor property page.
WIN32 WIN32_LEAN_AND_MEAN _WIN32_WINNT=0x0500 BOOST_ALL_NO_LIB _FILE_OFFSET_BITS=64 BOOST_THREAD_USE_LIB TORRENT_BUILDING_SHARED UNICODE TORRENT_USE_OPENSSL TORRENT_EXPORT - Add the to Additional Library Directories under Linker->General property page.
$(BOOST_ROOT)\stages\lib $(OpenSSL)\out32 - Add the followings to Additional Dependencies under Linker->Input property page.
wsock32.lib libboost_date_time-vc80-mt.lib libboost_filesystem-vc80-mt.lib libboost_system-vc80-mt.lib libboost_thread-vc80-mt.lib libeay32.lib ssleay32.lib - If you encounter the followings errors (like me)…
error C2039: 'function' : is not a member of 'boost' error C2061: syntax error : identifier 'function' error C2059: syntax error : ')' error C2143: syntax error : missing ')' before ';'You need to add #include <boost/function.hpp> into the following two files and perform a Rebuild to resolve the errors.
- .\include\libtorrent\storage.hpp
- .\include\libtorrent\torrent_handle.hpp
Testing libtorrent DLL
To test the DLL, simply follow the steps below.
- Create a Win32 Console Application project file. You need to select the "Empty project" checkbox at the Application Wizard dialog.
- Add any cpp file from C:\libtorrent\examples\ to the project
- Add the file dependencies to Additional Include Directories under C/C++->General property page. You can use the same one from Building libtorrent DLL.
- Add the folder where the libtorrent.lib resides into Additional Library Directories under Linker->General property page.
- Add libtorrent.lib to Additional Dependencies under Linker->Input property page.
- Build the application and voilà! You can test it now with any existing workable torrent.
Official libtorrent-rasterbar website
http://www.rasterbar.com/products/libtorrent/











Sooooooo awesome post, i love some words so much and can i quote a couple of of them on my blog? I also have e-mailed you regarding is it possible for us to exchange our links, hope hearing from you soon.
I want to thank the blogger very much not only for this post but also for his all previous efforts. I found softwerkz.net to be greatly interesting. I will be coming back to softwerkz.net for more information.