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

Building libmecab.dll on 64-bit Windows

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

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

The Problem

When using a 64-bit Ruby for Windows (x64-mingw32) or JRuby Windows executable x64 (Windows n-amd64) with the 32-bit build of mecab on Windows, the ffi binding will throw up mysterious and uninformative errors.

irb(main):001:0> ENV['MECAB_PATH']='C:\\Program Files (x86)\\MeCab\\bin\\libmecab.dll'

irb(main):002:0> require 'natto'
LoadError: Could not open library 'C:\Program Files (x86)\MeCab\bin\libmecab.dll': iM
    from C:/Ruby/Ruby200-x64/lib/ruby/gems/2.0.0/gems/ffi-1.9.6-x64-mingw32/lib/ffi/
        library.rb:133:in `block in ffi_lib'

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