Skip to content
Brooke M. Fujita edited this page Nov 12, 2015 · 14 revisions

Building libmecab.dll on 64-bit Windows

The MeCab Binary package for MS-Windows works fine when using a 32-bit build of Python for Windows.

But you may encounter problems as soon as you try using a 64-bit Windows Python with this 32-bit MeCab.

The Problem

When using a 64-bit Python for Windows (MSC v.1600 64 bit (AMD64)) with the 32-bit build of mecab on Windows, the cffi binding will throw up mysterious and uninformative errors.

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32

>>> import os
>>> os.environ['MECAB_PATH']='C:\\Program Files (x86)\\MeCab\\bin\\libmecab.dll'

>>> from natto import MeCab
>>> nm = MeCab()
Traceback (most recent call last):
  File "F:\Python\Python34\lib\site-packages\cffi\api.py", line 400, in _load_backend_lib
    return backend.load_library(name, flags)
OSError: cannot load library C:\Program Files (x86)\MeCab\bin\libmecab.dll: error 0xc1

The solution is to build a 64-bit libmecab.dll, which we will use instead of the 32-bit libmecab.dll in your existing MeCab installation.

A Solution

This is a bit involved, but stick with me.

  1. Download the Visual Studio 2015 Community Edition. During installation, select the Custom option to install Visual C++ (under Programming Languages).

  2. If you haven't done so already, download and install the MeCab Binary package for MS-Windows. This will also install the required mecab-ipadic system dictionary.

  3. Download the MeCab source mecab-0.996.tar.gz. Save to some temporary working folder, for example C:\temp\MeCab.

  4. Untar mecab-0.996.tar.gz, and cd into mecab-0.996\src.

  5. Patch for feature_index.cpp:

     # line 356, change cast from size_t to unsigned int
     
     before:  case 't':  os_ << (size_t)path->rnode->char_type;     break;
     after:   case 't':  os_ << (unsigned int)path->rnode->char_type;     break;
                                 ^^^^^^^^^^^^
    
  6. Patch for writer.cpp:

     # line 260, add cast to unsigned int
     
     before:  case 'L': *os << lattice->size(); break;
     after:   case 'L': *os << (unsigned int)lattice->size(); break;
                                ^^^^^^^^^^^^
    
  7. Patch for utils.h:

     # line 27, comment out unnecessary typedef for uint32_t
     
     before:  typedef unsigned long uint32_t;
     after:   /*typedef unsigned long uint32_t;*/
    
  8. Patch for Makefile.msvc.in:

     # line 6, set machine flag to X64
     
     before:  LDFLAGS = /nologo /OPT:REF /OPT:ICF /LTCG /NXCOMPAT /DYNAMICBASE /MACHINE:X86 ADVAPI32.LIB 
     after:   LDFLAGS = /nologo /OPT:REF /OPT:ICF /LTCG /NXCOMPAT /DYNAMICBASE /MACHINE:X64 ADVAPI32.LIB
                                                                                        ^^^
     
     # line 8, explicitly set DIC_VERSION value
     
     before:  -DDLL_EXPORT -DHAVE_GETENV -DHAVE_WINDOWS_H -DDIC_VERSION=@DIC_VERSION@ \
     after:   -DDLL_EXPORT -DHAVE_GETENV -DHAVE_WINDOWS_H -DDIC_VERSION=102 \ 
                                                                        ^^^
     
     # next, at line 9, explicitly set VERSION value
     
     before:  -DVERSION="\"@VERSION@\"" -DPACKAGE="\"mecab\"" \ 
     after:   -DVERSION="\"0.996\"" -DPACKAGE="\"mecab\"" \
                           ^^^^^
    
     # finally, at line 11, change the default path to mecabrc
     
     before:  -DMECAB_DEFAULT_RC="\"c:\\Program Files\\mecab\\etc\\mecabrc\""
     after:   -DMECAB_DEFAULT_RC="\"C:\\Program Files (x86)\\mecab\\etc\\mecabrc\""
                                    ^^^^^^^^^^^^^^^^^^^^^^^
    
  9. Set environment variables for building 64-bit libmecab.dll. Open up a Windows cmd window in the mecab-0.996\src folder, and execute:

     call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\vcvarsx86_amd64.bat"
    
  10. Next, from that same cmd window opened to mecab-0.996\src, execute nmake and build libmecab.dll:

     nmake -f Makefile.msvc.in
    
  11. Copy the resulting mecab-0.996\src\libmecab.dll and .exe files to your MeCab installation's bin dir (back up this folder beforehand, if you need to. Alternately, you could save the newly-built 64-bit libmecab.dll elsewhere, and use the MECAB_PATH environment variable to reference it.


References:


Previous | Home

Clone this wiki locally