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

September 26th, 2009 67 Commented

-> Last updated: 28/03/2012

To build libtorrent, we need the following components with their respective versions that this guide is using. To keep things simpler, OpenSSL has been omitted.

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.

Note: This version of Boost supports up to Microsoft Visual C++ 10.0

The steps are as follows.

  1. Unzip the boost file to e.g., D:\libtorrent\boost_1_49_0.
  2. Add the environment variable BOOST_ROOT and set its value to the boost folder
    e.g., D:\libtorrent\boost_1_49_0.
  3. Run the Command Prompt.
  4. Then, run the following commands.
    cd /d %BOOST_ROOT%
    bootstrap
    b2

    The bootstrap command will create b2.exe. Once the bootstrap is completed, we run the b2 command to build the boost source.

    This will take some time. So, let’s have a cup of coffee while waiting for it.


Building libtorrent DLL

Assuming that the libtorrent file is unzip to D:\libtorrent\libtorrent-rasterbar-0.15.8.

  1. Add the environment variable LIBTORRENT and set its value to the libtorrent folder
    e.g., D:\libtorrent\libtorrent-rasterbar-0.15.8.
  2. Create the Win32 Project file (e.g. residing in D:\libtorrent).
  3. 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.
  4. Add all the source files in D:\libtorrent\libtorrent-rasterbar-0.15.8\src to your project, including those in its sub-directories. The header files in D:\libtorrent\libtorrent-rasterbar-0.15.8\include\libtorrent can be added too for easy reference.

    Note: Steps 5 to 7 involves Project->Properties.

  5. Add the followings to Additional Include Directories under C/C++->General property page.
    $(BOOST_ROOT)
    $(LIBTORRENT)\include
    $(LIBTORRENT)\include\libtorrent
    $(LIBTORRENT)\zlib
    
  6. Add the followings to Preprocessor Definitions under C/C++->Preprocessor property page.
    WIN32
    WIN32_LEAN_AND_MEAN
    _WIN32_WINNT=0x0500
    _CRT_SECURE_NO_DEPRECATE
    UNICODE
    BOOST_ASIO_HASH_MAP_BUCKETS=1021
    BOOST_FILESYSTEM_VERSION=2
    WITH_SHIPPED_GEOIP_H
    TORRENT_DISABLE_ENCRYPTION
    TORRENT_BUILDING_SHARED
    
  7. Add the followings to Additional Library Directories under Linker->General property page.
    $(BOOST_ROOT)\stage\lib
    


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 D:\libtorrent\libtorrent-rasterbar-0.15.8\examples\ to the project
  3. Add the followings to Preprocessor Definitions under C/C++->Preprocessor property page.
    BOOST_FILESYSTEM_VERSION=2
    
  4. Add the file dependencies to Additional Include Directories under C/C++->General property page. You can use the same one from Building libtorrent DLL.
  5. Add the folder where the libtorrent.lib resides into Additional Library Directories under
    Linker->General property page.
  6. Add libtorrent.lib to Additional Dependencies under Linker->Input property page.
  7. Build the application and voilà! You can test it now with any existing workable torrent.
Note: The examples application may look unpleasant (I don’t own them). Feel free to change the examples code and experience your changes.



Lastly, if you’re having difficulty following the tutorial mentioned above, here is the solution and project files. They are created using Visual C++ 9.0. These files reside in the .\libtorrent folder (refer to the image below).

Note: These project files are created by going through the steps above. You’d need the environment variables for them to compile correctly.



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:
, , , ,

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

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

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

  5. Luna-Tic says:

    I have this error to, my solution was perl Configure VC-WIN32 -–prefix=”dir” like linux\bsd — not – (one)

  6. LoopBack says:

    Can you give me the libtorrent.lib and other libs that can use under the windows?

    e-mail:dll8874@gmail.com
    thank you

    • softz says:

      Hi, I just sent your the libtorrent.lib. Sorry for the delay. I was in overseas. If I remember correctly, it’s a DLL. So, all libraries should be in the DLL. Hope it helps.

  7. cb says:

    Thanks. I built the DLL using VC 2010.

  8. KMVS says:

    Hi,

    I’m very new to libtorrent and I am trying to build libtorrent 0_15_5 using

    boost_1_46_1
    msvc 2010 express

    All though, I could build dll. When I try to test using examples, the test program throws errors

    for example, If I try to test make_torrent.

    I get the following errors
    1> make_torrent.cpp
    1>d:\libtorrent\include\libtorrent\file_storage.hpp(110): error C2572: ‘libtorrent::file_storage::add_file’ : redefinition of default parameter : parameter 5
    1> d:\libtorrent\include\libtorrent\file_storage.hpp(106) : see declaration of ‘libtorrent::file_storage::add_file’
    1>d:\libtorrent\include\libtorrent\file_storage.hpp(110): error C2572: ‘libtorrent::file_storage::add_file’ : redefinition of default parameter : parameter 4
    1> d:\libtorrent\include\libtorrent\file_storage.hpp(106) : see declaration of ‘libtorrent::file_storage::add_file’
    1>d:\libtorrent\include\libtorrent\file_storage.hpp(110): error C2572: ‘libtorrent::file_storage::add_file’ : redefinition of default parameter : parameter 3
    1> d:\libtorrent\include\libtorrent\file_storage.hpp(106) : see declaration of ‘libtorrent::file_storage::add_file’
    1>d:\libtorrent\include\libtorrent\file_storage.hpp(110): error C2535: ‘void libtorrent::file_storage::add_file(const boost::filesystem3::path &,libtorrent::size_type,int,time_t,const boost::filesystem3::path &)’ : member function already defined or declared
    1> d:\libtorrent\include\libtorrent\file_storage.hpp(106) : see declaration of ‘libtorrent::file_storage::add_file’
    1>d:\libtorrent\include\libtorrent\file_storage.hpp(110): error C2548: ‘libtorrent::file_storage::add_file’ : missing default parameter for parameter 3
    1>d:\libtorrent\include\libtorrent\file_storage.hpp(110): error C2548: ‘libtorrent::file_storage::add_file’ : missing default parameter for parameter 4
    1>d:\libtorrent\include\libtorrent\file_storage.hpp(110): error C2548: ‘libtorrent::file_storage::add_file’ : missing default parameter for parameter 5
    1>d:\libtorrent\include\libtorrent\file_storage.hpp(173): fatal error C1903: unable to recover from previous error(s); stopping compilation
    Please let me know, If I am doing any mistake.

    Cheers,
    KMVS

  9. Geomma says:

    you could me send the Visual Studio Project Files?
    I had many problems to compile in VC10.0

  10. Geomma says:

    hi
    i get the following errors.

    ——————-

    > nmake -f ms/nt.mak

    Building OpenSSL
    cl /Fotmp32\cryptlib.obj -Iinc32 -Itmp32 /MT /Ox /O2 /Ob2 -DOPENSSL_THREADS -DDSO_WIN32 -W3 -Gs0 -GF -Gy -nolo
    go -DOPENSSL_SYSNAME_WIN32 -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -D_CRT_SECURE_NO_DEPRECATE -DOPENSSL_BN_ASM_PART_WORDS -DOPE
    NSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DRMD160_ASM -DAES_ASM -DWHIRLPOOL_A
    SM -DOPENSSL_NO_RC5 -DOPENSSL_NO_MD2 -DOPENSSL_NO_KRB5 -DOPENSSL_NO_JPAKE -DOPENSSL_NO_DYNAMIC_ENGINE /Zl /Zi /Fdtmp32/l
    ib -c .\crypto\cryptlib.c
    NMAKE : fatal error U1077: ‘.\cl.EXE’ : return code ’0xc0000135′
    Stop.

    —————

    please help me…

  11. WalkMan says:

    Hi! Do you know how to build it all for python? I read all manuals on official libtorrent site, but i have some problems with one of this part.

  12. Tim Trahan says:

    Not to be a me also, but I have tried compiling this library and have been unable to do so. I am writing an app that will use a bittorrent engine to download files. I am using visual studio 2010. Can you send me the compiled dll’s to ttrahan@comcast.net? I have gotten monotorrent to work, but it seems quite buggy. Is there a place to go to get the pre-compiled dlls for this or does everybody just figure out how to build there own?

    Thanks!
    Tim

    • softz says:

      The last time I didn’t notice any pre-compiled dlls. I don’t know about it now. But, I’ll send you the compiled files and the project files (you need the .h file for your applications).

  13. M. Bedda says:

    Hello,

    I’ve had a trouble going smoothly through the above steps, can you send me the dll’s on mbedda@techberg.com. Thanks.

    • M. Bedda says:

      Managed to save at the end thanks man for your tutorial, it was really helpful.

      • M. Bedda says:

        I mean managed to build

      • softz says:

        I’m glad to hear that you managed to build it. Sorry I was busy and didn’t notice your comments. Let me know if you’d still need anything. :)

        • M. Bedda says:

          Did u try building the client_test example? I’m getting lots of errors like this one:

          Error 119 error LNK2019: unresolved external symbol “bool __cdecl libtorrent::from_hex(char const *,int,char *)” (?from_hex@libtorrent@@YA_NPBDHPAD@Z) referenced in function _main C:\libtorrent\client_test\client_test\client_test.obj client_test

          I’m trying to use this library within a c# application, did you try something like that out? I didn’t use c++ libraries in c#, do you think I would be able to work that out at the end?

          • softz says:

            Hi,

            Yep, I tried the client_test example and it worked fine. For that error, you might want to check if your libtorrent.lib is included into the test project. The compiler is complaining that it couldn’t find the code for the function from_hex(char const *,int,char *)

            To support C# application, you might need to do quite a bit of marshaling work since it’s not a COM object. However, you can always try MonoTorrent as an alternative, it is written in C# and I guess it’d serve your purpose well.

  14. ryandaniels says:

    Hey, sorry if I’m being dense, but there seems to be step you left out;

    I couldn’t find the C/C++ section in the properties page, so I searched around, and I just discovered that this section does not show up until you have at least one .cpp file.

    SO my question is: after I make a project in libtorrent/win32, how exactly am I supposed to get all the headers and .cpp files to show up in my project folders (presumably so they can actually compile)? I could try to do it manually, but then I lose directory structure… Basically, if I follow your steps exactly, I just end up with a empty project.

    Other than this hiccup this tutorial has been great, so thanks!

    • ryandaniels says:

      Well, after retrying with a blank foo.cpp file, it worked.

    • softz says:

      Hi,

      I’m glad you found the solutions. Yep, you need to have at least one cpp file in order for you to access the C/C++ section properties. Probably, I should state that in the steps too. Sorry about that and I hope the library serves you well.

      • ryandaniels says:

        Thanks for the quick reply; however I now have a new problem. I was able to compile a dll and lib file, but I’m not sure they contain any actual useful information.

        I attempted to compile one of the sample programs, and I eventually ran into the expected linker error referencing a bunch of libtorrent stuff. So, I went ahead and added libtorrent.lib to my additional dependencies (and added the library folder), and to my surprise, none of the linker errors went away.

        I noticed that when compiling libtorrent, it goes quite quickly, and it turns out I can remove pretty much any seemingly vital settings (such as all the additional dependencies) and it still compiles fine. So obviously nothing is actually going into my lib or dll files when I compile libtorrent.

        I tried also adding the libtorrent/src folder to the “Source Directories” list, no effect. I think I’m missing something important here, so I’m sorry for what will probably be an embarrassingly simple and obvious mistake.

        • softz says:

          If you are trying to compile the test source code provided by libtorrent, do take note that they are using lots of boost. So, you may need to include the respective headers and libraries of boost if you haven’t done so. It’ll help if you could let me know what are the exact errors.

  15. Hesham says:

    Hello,

    Could you please send me the compiled DLL by mail???

    Thanks in advance!

  16. George says:

    Anybody knows how do I make a DLL that works in C#?

  17. autojack says:

    Pls i need project files in vs2005 or 08

  18. Francesco says:

    Hi,

    I’m very new to libtorrent and I am trying to build libtorrent 0_15_5 using Boost library 1.40 and libtorrent library 0.15.8

    During the building boost library,at the step 4 i get the following errors:

    Building Boost.Jam build engine
    Can not find the file specified.
    “cl” is not recognized as internal or external command,
    an executable program or batch file.

    Failed to build Boost.Jam build engine.
    Please consult bjam.log for furter diagnostics.

    You can try to obtain a prebuilt binary from

    http://sf.net/project/showfiles.php?group_id=7586&package_id=72941

    Also, you can file an issue at http://svn.boost.org
    Please attach bjam.log in that case.

    please help me..

  19. thienlong says:

    I built libtorrent dll and simple_client in libtorrent/samples. But i got error when run it: “The program can’t start because torrent.dll is missing from your computer. Try reinstalling the program to fix this problem”
    Please help me..

    • softz says:

      Hi, is your working directory set to the appropriate folder? For simplicity, the dll and exe should be in the same folder. So, if your files resides in C:\torrent, then when you run your command prompt, the directory should be set to C:\torrent. Of course, you can also set the PATH. But, the previous one is simpler.

  20. tim says:

    hi, i tried to build libtorrent, i created an empty .cpp (in order to show to me c++ properties). Then i build the project, i went into project folder but i couldn’t find libtorrent.lib,
    what i am doing wrong? how create libtorrent.lib?
    Thanks..

    • softz says:

      Hi Tim, my bad… I realized that I’ve missed out a step. Just updated it. You need not create an empty .cpp, you’d need to add the libtorrent source files to your project and set the properties. You can refer to the updated steps. Thanks for alerting.

      • tim says:

        ok ,thanks for answer. It works but i have now another problem. I build my project with VS2008 and it starts compile the files but finally it show me an error
        (c:\libtorrent\libtorrent-rasterbar-0.15.8\src\ut_pex.cpp : error C2471: cannot update program database ‘c:\libtorrent\build\obj\debug\libtorrent\vc90.pdb’
        c:\libtorrent\libtorrent-rasterbar-0.15.8\src\ut_pex.cpp : fatal error C1083: Cannot open program database file: ‘c:\libtorrent\build\obj\debug\libtorrent\vc90.pdb’: No such file or directory),
        i went into debug\libtorrent folder and i saw that vc90.pdb exists. What is going wrong?

        • softz says:

          Try “Clean Solution” to delete all the object files.
          If problem still persists, you can try closing VS and delete the build folder manually.

          • tim says:

            Thanks for answer, is there any other way to use libtorrent? If you send me .lib and .dll file it will works?

          • softz says:

            You can refer to the client code to understand how to use libtorrent. You can also run the simple_client.exe with a torrent file as an argument and see the torrent library in action.

            You can download my solution and project files listed above. It will build the library and sample clients showing how the libtorrent is used. It’s a VS2008 solution and it pretty straight forward.

            Do let me know if you encounter any issues.

          • tim says:

            I tried to compline your solution, it runs forn about 2 minutes but then tells build failed.. :-(

            c:\libtorrent\libtorrent-rasterbar-0.15.8\include\libtorrent\entry.hpp(81): warning C4275: non dll-interface class ‘std::runtime_error’ used as base for dll-interface struct ‘libtorrent::type_error’
            1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdexcept(158) : see declaration of ‘std::runtime_error’
            1> c:\libtorrent\libtorrent-rasterbar-0.15.8\include\libtorrent\entry.hpp(80) : see declaration of ‘libtorrent::type_error’
            1>c:\libtorrent\boost_1_49_0\boost\exception\detail\exception_ptr.hpp(17): fatal error C1189: #error : This header requires exception handling to be enabled.
            1> Generating Code…
            1>
            1>Build FAILED.
            1>
            1>Time Elapsed 00:02:00.64
            ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

          • softz says:

            I see… You’re getting this error because you’re using boost_1_49_0 instead of the one I listed boost_1_40_0.

            Ok, if you want to use boost_1_49_0, you need to remove BOOST_NO_EXCEPTIONS from the Preprocessor Definition.

            Also, you need to add BOOST_FILESYSTEM_VERSION=2 to the Preprocessor Definition in all the sample client projects in the solution.

            These should solve your issues. Also, all the projects use UNICODE.

            I’ll try to update this page to use the latest Boost when time permits. :D

          • tim says:

            Thanks, it works finally.
            I also build the simple_client project.. but when i try to run it, it
            says :
            ‘simple_client.exe’: Loaded ‘C:\libtorrent\Build\bin\Debug\simple_client.exe’, Symbols loaded.
            ‘simple_client.exe’: Loaded ‘C:\Windows\SysWOW64\ntdll.dll’, Symbols loaded (source information stripped).
            ‘simple_client.exe’: Loaded ‘C:\Windows\SysWOW64\kernel32.dll’, Symbols loaded (source information stripped).
            ‘simple_client.exe’: Loaded ‘C:\Windows\SysWOW64\KernelBase.dll’, Symbols loaded (source information stripped).
            ‘simple_client.exe’: Loaded ‘C:\libtorrent\Build\bin\Debug\libtorrent.dll’, Symbols loaded.
            ‘simple_client.exe’: Loaded ‘C:\Windows\SysWOW64\ws2_32.dll’, Symbols loaded (source information stripped).
            The program ‘[4124] simple_client.exe: Native’ has exited with code 1 (0×1).

          • tim says:

            I find it, sorry. I run it from cmd and it works..
            Thanks for the support :-)

          • softz says:

            Great to know you’ve got it running :D

Leave a Reply


Captcha:    6 − = three