How-To: Build libtorrent DLL for Windows in C++

September 26th, 2009 10 Commented

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.

  1. Unzip the boost file to e.g., C:\boost.
  2. Add the environment variable BOOST_ROOT and set its value to the boost folder e.g., C:\boost.
  3. Run the Visual Studio <version> Command Prompt.
  4. Then, run the following commands.
    cd %BOOST_ROOT%
    bootstrap
    bjam

    The 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

  1. Unzip the openssl file to e.g., C:\OpenSSL
  2. Add an environment variable called OpenSSL and set its value to the the boost folder e.g., C:\OpenSSL.
  3. Install the ActivePerl if you have not.
  4. Unzip the nasm file and add the folder to the Path environment variable.
  5. 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.mak
    

    Note that if you get error C2220: warning treated as error after running nmake -f ms\nt.mak, do the following three steps.

    1. Open up the file ms\nt.mak.
    2. Remove /WX at CFLAG options.
    3. Then run nmake -f ms\nt.mak again.


Building libtorrent DLL

Assuming that the libtorrent file is unzip to C:\libtorrent.

  1. Create the Win32 Project file (e.g. residing in C:\libtorrent\win32).
  2. 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.

  3. 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
    
  4. 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
    
  5. Add the to Additional Library Directories under Linker->General property page.
    $(BOOST_ROOT)\stages\lib
    $(OpenSSL)\out32
    
  6. 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
    
  7. 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.

  1. Create a Win32 Console Application project file. You need to select the "Empty project" checkbox at the Application Wizard dialog.
  2. Add any cpp file from C:\libtorrent\examples\ to the project
  3. Add the file dependencies to Additional Include Directories under C/C++->General property page. You can use the same one from Building libtorrent DLL.
  4. Add the folder where the libtorrent.lib resides into Additional Library Directories under Linker->General property page.
  5. Add libtorrent.lib to Additional Dependencies under Linker->Input property page.
  6. Build the application and voilà! You can test it now with any existing workable torrent.
Note: The examples application may look unpleasing. Feel free to change the examples code and experience your changes.


Official libtorrent-rasterbar website
http://www.rasterbar.com/products/libtorrent/


Possibly related posts: (automatically generated)
  1. Static Link Library (SLL) vs. Dynamic Link Library (DLL) in C/C++
  2. Memory Leak detection with Microsoft Visual C++ – Part 1

Tags:
, , , ,

10 responses to “How-To: Build libtorrent DLL for Windows in C++”

  1. Brad says:

    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!

  2. softz says:

    Hi Brad,
    May I know what are the errors you got? Also, where you using /MT or /MD to do your compilation?

  3. nitpe says:

    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

  4. Phil says:

    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?

    • softz says:

      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).

  5. Joon says:

    Step 6 for building libtorrent DLL. When linking those libraries, I could not locate libeay32.lib or ssleay32.lib, is this a problem?

    • softz says:

      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.

      • Joon says:

        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!

        • softz says:

          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:

          
          Configuring for VC-WIN32
              no-gmp          [default]  OPENSSL_NO_GMP (skip dir)
              no-jpake        [experimental] OPENSSL_NO_JPAKE (skip dir)
              no-krb5         [krb5-flavor not specified] OPENSSL_NO_KRB5
              no-md2          [default]  OPENSSL_NO_MD2 (skip dir)
              no-rc5          [default]  OPENSSL_NO_RC5 (skip dir)
              no-rfc3779      [default]  OPENSSL_NO_RFC3779 (skip dir)
              no-shared       [default]
              no-store        [experimental] OPENSSL_NO_STORE (skip dir)
              no-zlib         [default]
              no-zlib-dynamic [default]
          IsMK1MF=1
          CC            =cl
          CFLAG         =-DOPENSSL_THREADS  -DDSO_WIN32 -W3 -WX -Gs0 -GF -Gy -nologo -DOPENSSL_SYSNAME_WIN32 -DWIN32_LEAN_AND_MEAN -DL_ENDIAN ... < more of the definitions here >
          EX_LIBS       =
          CPUID_OBJ     =x86cpuid.o
          BN_ASM        =bn-586.o co-586.o x86-mont.o
          DES_ENC       =des-586.o crypt586.o
          AES_ENC       =aes-586.o
          BF_ENC        =bf-586.o
          CAST_ENC      =cast-586.o
          RC4_ENC       =rc4-586.o
          RC5_ENC       =rc5-586.o
          MD5_OBJ_ASM   =md5-586.o
          SHA1_OBJ_ASM  =sha1-586.o sha256-586.o sha512-586.o
          RMD160_OBJ_ASM=rmd-586.o
          CMLL_ENC=     =cmll-x86.o
          PROCESSOR     =
          RANLIB        =true
          ARFLAGS       =
          PERL          =perl
          THIRTY_TWO_BIT mode
          BN_LLONG mode
          RC4_INDEX mode
          RC4_CHUNK is undefined
          
          Configured for VC-WIN32.

          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:

          
          #if !defined(OPENSSL_IMPLEMENTS_strncasecmp) && \
              !defined(OPENSSL_SYSNAME_WIN32) && \
              !defined(NETWARE_CLIB)
          # include 
          #endif

          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.

Leave a Reply

CommentLuv Enabled