How-To: Build libtorrent DLL for Windows in C++
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/
This is awesome! But I can’t get it working. I’m trying to build libtorrent as a static library, but I keep getting errors (which the debugger cannot catch). The libtorrent website says this:
If you link statically to the runtime library, you cannot build libtorrent as a shared library (DLL), since you will get separate heaps in the library and in the client application. It will result in crashes and possibly link errors.
I might be doing it wrong. If you can show me how to make a static version that would be awesome!
Hi Brad,
May I know what are the errors you got? Also, where you using /MT or /MD to do your compilation?
Hello, you could me send the compiled libtorrent.lib and libtorrentd.lib ?
I had thousands of problems to compile in VC9.0
my email is:
power.eptin [a] gmail . com
thanks
Sure thing, I’ve sent you the library for both debug and release versions. Hope they help.
Hey, for testing the libtorrent library, for steps 4/5 where is the libtorrent.lib located. Did we generate that when making the libtorrent DLL?
Hi Phil,
It should reside in the folder where your libtorrent.dll located. It is created/compiled when you build the libtorrent DLL. My guess, if you didn’t change your project settings, it should be in the debug/release folder of in your solution folder (depending which configuration you build it with).
Step 6 for building libtorrent DLL. When linking those libraries, I could not locate libeay32.lib or ssleay32.lib, is this a problem?
Hi Joon,
If you have gone through the building of OpenSSL library, you’ll find libeay32.lib and ssleay32.lib in $(OpenSSL)\out32, depending on where you placed your OpenSSL folder (e.g. C:\OpenSSL\out32). Let me know if you have any problem getting that.
Of course, the compilation of OpenSSL can be removed by specifying a flag in the Preprocessor Definitions. However, I won’t go into it now. If you want to know more, do let me know.
Hi, I have gone through OpenSSL, but I can’t find libeay32lib and ssleay32.lib in $(OpenSSL)\out32. hm… weird.
oh I just found an error after I ran nmake -f ms\nt.mak
cl : Command line warning D9035 : option ‘O’ has been deprecated and will be rem
oved in a future release
o_str.c
.\crypto\o_str.c(66) : fatal error C1083: Cannot open include file: ‘strings.h’:
No such file or directory
NMAKE : fatal error U1077: ‘”C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN
\cl.EXE”‘ : return code ’0×2′
Stop.
Thanks!
Hi Joon,
Did you run “perl Configure VC-WIN32 –prefix=%OpenSSL%” with Visual Studio 2008 Command Prompt under the directory of your OpenSSL folder?
Example (for my test case here):
D:\openssl-1.0.0a>perl Configure VC-WIN32 –prefix=%OpenSSL%
Your results will be something like the following:
If you look at the CFLAG values, you will find -DOPENSSL_SYSNAME_WIN32, this actually disable the #include in the o_str.c
If look into the file o_str.c, you’ll see:
So two checks here. First make sure you are running the command under Visual Studio Command Prompt and the CFLAG has the mentioned flag.
Also, you can open up the Configure file in the OpenSSL folder. Do a find on “OPENSSL_SYSNAME_WIN32″, you will see it in VC-WIN32 target. You can add it there or re-download the OpenSSL if it’s not there.
Hope it helps.