
.. blogpost::
    :title: Building dlib for Python 3.6 and boost_python on Windows
    :keywords: dlib, Windows, boost_python, vcpkg
    :date: 2017-07-10
    :categories: module

    :epkg:`dlib` is quite difficult to build on
    Windows for Python 3.6. A compiled version exists on
    `PyPi/dlib <https://pypi.python.org/pypi/dlib>`_ for Python 3.5
    but the version for Python 3.6 remains unavailable unless you
    are using Anaconda:
    `Installing Dlib on Anaconda Python on Windows <http://www.codesofinterest.com/2016/10/installing-dlib-on-anaconda-python-on.html>`_.
    The blog `Install Dlib on Windows <http://www.learnopencv.com/install-dlib-on-windows/>`_
    even recommends not to try and to use *conda* (with Anaconda) instead:

    ::

        conda install -c conda-forge dlib=19.4

    It seems easier to do it on Ubuntu
    `Install dlib on Ubuntu <http://www.learnopencv.com/install-dlib-on-ubuntu/>`_.
    As I was still trying to go on with the compilation, I discovered this tool
    `vcpkg <https://blogs.msdn.microsoft.com/vcblog/2016/09/19/vcpkg-a-tool-to-acquire-and-build-c-open-source-libraries-on-windows/>`_
    which helps installing C++ packages on Windows.
    I followed the instructions to build it:

    ::

        git clone https://github.com/Microsoft/vcpkg
        powershell -exec bypass scripts\bootstrap.ps1

    And I compiled boost:

    ::

        vcpkg install boost:x64-windows

    Be patient because installing
    :epkg:`boost` takes a while because it builds everything.
    One issue though:
    it does not include Python:
    `issue 78 <https://github.com/Microsoft/vcpkg/issues/78>`_.
    The same instruction works for *dlib*:

    ::

        vcpkg install dlib:x64-windows

    But it is yet impossible to build the python extension *dlib* for Windows.
    I had to start from scratch.
    So I started to build :epkg:`boost_python`
    on my own. The tutorial is easy to follow but it took a week
    to find the good page telling how to get the 64 bits version of it.
    The trick I was missing is to build :epkg:`boost_python`
    from a *VS2015 x64 Native Tools Command Prompt* otherwise
    the builds gets stuck in 32 bits.

    * `Boost Build <http://www.boost.org/doc/libs/1_64_0/doc/html/bbv2/reference.html#bbv2.reference.tools.compiler.hp_cxx>`_
    * `How to build boost Version 1.58.0 using Visual Studio 2015 (Enterprise) <https://stackoverflow.com/questions/31566807/how-to-build-boost-version-1-58-0-using-visual-studio-2015-enterprise>`_

    This part is summarize in the script
    `build_boost_python_static.bat <https://github.com/sdpython/pymyinstall/blob/master/whl/windows/build_boost_python_static.bat>`_.
    Then comes :epkg:`dlib`. I also followed the tutorial which explains
    how to build the C++ version of it. No issue there. The first part
    starts with:

    ::

        python -u setup.py build_ext --inplace --yes USE_AVX_INSTRUCTIONS
        python -u setup.py bdist_wheel

    If fails to find the Python static libraries because the linker
    cannot find them and *boost_python* because the name is different.
    So I copied tham at the same location as *boost_python* build.

    ::

        set version=1_64_0
        set BOOST_LIBRARYDIR=something like ...\boost_%version%\boost_%version%\stage\x64\lib
        copy %pythonexe%\libs\*.lib %BOOST_LIBRARYDIR%
        copy %BOOST_LIBRARYDIR%\libboost_python3-vc140-mt-s-1_64.lib %BOOST_LIBRARYDIR%\libboost_python-vc140-mt-s-1_64.lib
        copy %BOOST_LIBRARYDIR%\libboost_numpy3-vc140-mt-s-1_64.lib %BOOST_LIBRARYDIR%\libboost_numpy3-vc140-mt-s-1_64.lib

    The full process is given by
    `build_dlib.bat <https://github.com/sdpython/pymyinstall/blob/master/whl/windows/build_dlib.bat>`_.
    It was quite a long journey. You can find my build
    at `dlib-19.4.99-cp36-cp36m-win_amd64.whl <http://www.xavierdupre.fr/enseignement/setup/dlib-19.4.99-cp36-cp36m-win_amd64.whl>`_.

    More about `vcpkg <https://docs.microsoft.com/en-us/cpp/vcpkg>`_:

    * `Microsoft documentation <https://docs.microsoft.com/en-us/cpp/vcpkg>`_
    * `github/vcpkg <https://github.com/Microsoft/vcpkg>`_
    * `documentation vcpkg <https://vcpkg.readthedocs.io/en/latest/>`_
    * `list of available packages <https://github.com/Microsoft/vcpkg/tree/master/ports>`_
