This document describes step-by-step instructions for building MeCab wrapper library refered by the bindings from Smdn.Text.Ondulish.
The following instructions are an example in a Linux environment, so replace them according to the target environment.
Clone the MeCab source tree to the directory mecab
.
git clone https://github.com/taku910/mecab.git --depth 1 mecab
Firstly, run configure
on MeCab root directory.
cd mecab/mecab/
./configure --enable-shared --enable-static -with-pic --with-charset=utf8 --enable-utf8-only
cd -
Then, build a MeCab shared library.
cd mecab/mecab/src/
make CXXFLAGS="-std=c++14 -fPIC -O3" libmecab.la
cd -
Firstly, create a directory where the C# bindings will be output.
We don't use the C# bindings generated by SWIG here, but SWIG requires the output directory.
mkdir -p mecab-bindings-csharp
Next, generate a source of the wrapper.
The options -namespace MeCab
and -dllimport mecab
will be the names referenced by binding of DllImport
, so do not change them.
swig -c++ -o MeCab_wrapper.cxx -csharp -namespace MeCab -dllimport mecab -outdir mecab-bindings-csharp mecab/mecab/swig/MeCab.i
Finally, compile the wrapper and MeCab shared library to create a wrapper library.
The output filename libmecab.so
should be changed according to the target platform. As an example, it should be mecab.dll
on Windows and libmecab.dylib
on macOS.
g++ -std=c++14 -fPIC -O3 -c MeCab_wrapper.cxx -I'mecab/mecab/src/' -o MeCab_wrapper.o
g++ -std=c++14 -fPIC -O3 -shared -s MeCab_wrapper.o mecab/mecab/src/.libs/libmecab.a -o libmecab.so
Deploy the built wrapper library in the same directory as Smdn.Text.Ondulish.dll
.
If you encounter the exceptions like below, check the following:
- Whether the libraries required by MeCab are installed in the environment where the wrapper library is deployed.
- Whether the file name/extension of the wrapper library matches the file name/extension of the library in the execution environment. (
libmecab.so
,mecab.dll
,libmecab.dylib
, etc.) - Whether the wrapper library and
Smdn.Text.Ondulish.dll
are deployed in the same directory or one of theDllImport
's search directories. - Whether the function name referenced from
DllImport
ofSmdn.Text.Ondulish.dll
matches the function name in the wrapper library. In other words, are the namespace and the DllImport name specified to SWIG correct?
Unhandled exception. System.TypeInitializationException: The type initializer for 'MeCab.MeCabPINVOKE' threw an exception.
---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception.
---> System.DllNotFoundException: Unable to load shared library 'mecab' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libmecab: cannot open shared object file: No such file or directory