pipでmojimojiをインストールできない問題について

# 環境
- Mac OS X 10.14.6
- pyenv 1.2.14
- Python 3.7.0(anaconda3-5.3.1)
- pip 19.3.1

# 発生した問題
## mojimojiのインストールでgccエラー
※ここから先は,私の試したことを辿るので,お急ぎの方やサクッと解決法を知りたい方は[解決法まとめ](#解決法まとめ)へお願いします.

かなり省略していますが以下のようにエラーが出ました.

```console
$ pip install mojimoji
...
  ERROR: Command errored out with exit status 1:
...
  mojimoji.cpp:581:10: fatal error: 'ios' file not found
  #include "ios"
           ^~~~~
  1 warning and 1 error generated.
  error: command 'gcc' failed with exit status 1
...
```

# 試した方法
## 第一段階
調べてみると,まず以下の方法が見つかりました.
参考：https://qiita.com/nkmrtkhd/items/eac5828d031c72446e76

```console
$ cd $HOME
$ git clone https://github.com/studio-ousia/mojimoji
$ cd mojimoji
$ python setup.py install
```
ウン！やってみよう！

```console
$ python setup.py install
running install
running bdist_egg
running egg_info
creating mojimoji.egg-info
writing mojimoji.egg-info/PKG-INFO
writing dependency_links to mojimoji.egg-info/dependency_links.txt
writing top-level names to mojimoji.egg-info/top_level.txt
writing manifest file 'mojimoji.egg-info/SOURCES.txt'
reading manifest file 'mojimoji.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'mojimoji.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-10.7-x86_64/egg
running install_lib
running build_ext
building 'mojimoji' extension
creating build
creating build/temp.macosx-10.7-x86_64-3.7
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/include -arch x86_64 -I/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/include -arch x86_64 -I/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/include/python3.7m -c mojimoji.cpp -o build/temp.macosx-10.7-x86_64-3.7/mojimoji.o -std=c++11
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard
      library instead [-Wstdlibcxx-not-found]
mojimoji.cpp:581:10: fatal error: 'ios' file not found
#include "ios"
         ^~~~~
1 warning and 1 error generated.
error: command 'gcc' failed with exit status 1
```
^_^
エラーは変わりませんでした.

## 第二段階
第一段階の後しばらく調べてみたのですが結局変わらず...

方向転換して(と言うか解決した今では当たり前なのかもしれませんが...),mojimojiのエラーとしてではなく,#include "ios"でのエラーを解決する方向で調べると以下のページにたどり着きました.
参考：https://github.com/cleardusk/3DDFA/issues/57

簡単に言えば,Mac使ってるならsetup.pyで`extra_compile_args=['-std=c++11', '-D_hypot=hypot', '-stdlib=libc++']`にしてみろとのこと.

```python:setup.py
# -*- coding: utf-8 -*-

import io
import sys
from setuptools import setup, Extension

if sys.platform.startswith('win'):
    compile_args = []
    link_args = []
else:
    compile_args = ['-std=c++11']
    link_args = ['-std=c++11']


setup(
    # 省略
)
```
中身はこんな感じ.
else分のところにcompile_argsがありますね.
これを先ほどのextra_compile_argsに書き換えましょう.

```python:setup.py
# -*- coding: utf-8 -*-

import io
import sys
from setuptools import setup, Extension

if sys.platform.startswith('win'):
    compile_args = []
    link_args = []
else:
    compile_args=['-std=c++11', '-D_hypot=hypot', '-stdlib=libc++'] # New!
    link_args = ['-std=c++11']


setup(
    # 省略
)
```
注意ですが,変数名もextra_compile_argsにしないようにしてください.
やっちゃうと動きません.私は~~天才~~アホなのでやって動きませんでした.

と言う訳で動かしてみます.

```console
$ python setup.py install
running install
running bdist_egg
running egg_info
writing mojimoji.egg-info/PKG-INFO
writing dependency_links to mojimoji.egg-info/dependency_links.txt
writing top-level names to mojimoji.egg-info/top_level.txt
reading manifest file 'mojimoji.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'mojimoji.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-10.7-x86_64/egg
running install_lib
running build_ext
building 'mojimoji' extension
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/include -arch x86_64 -I/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/include -arch x86_64 -I/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/include/python3.7m -c mojimoji.cpp -o build/temp.macosx-10.7-x86_64-3.7/mojimoji.o -stdlib=libc++ -mmacosx-version-min=10.9 -std=c++11 -D_hypot=hypot
g++ -bundle -undefined dynamic_lookup -L/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/lib -arch x86_64 -L/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.7-x86_64-3.7/mojimoji.o -o build/lib.macosx-10.7-x86_64-3.7/mojimoji.cpython-37m-darwin.so -std=c++11
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]
ld: library not found for -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'g++' failed with exit status 1
```
えぇ...
もう一度先ほどの[Issue](https://github.com/cleardusk/3DDFA/issues/57)をみてみると,下の方に書いてありました.
こう言うのはちゃんと読みましょう.（ブーメラン）
`extra_link_args=['-stdlib=libc++', '-mmacosx-version-min=10.9','-std=c++11', '-D_hypot=hypot']`もやろうねってことでしたので変更を.

```python:setup.py
# -*- coding: utf-8 -*-

import io
import sys
from setuptools import setup, Extension

if sys.platform.startswith('win'):
    compile_args = []
    link_args = []
else:
    compile_args=['-std=c++11', '-D_hypot=hypot', '-stdlib=libc++'] # New!
    link_args=['-stdlib=libc++', '-mmacosx-version-min=10.9','-std=c++11', '-D_hypot=hypot'] # New!


setup(
    # 省略
)
```
先ほどと同様にextraはつけないように.

これを動かすと

```console
$ python setup.py install
running install
running bdist_egg
running egg_info
writing mojimoji.egg-info/PKG-INFO
writing dependency_links to mojimoji.egg-info/dependency_links.txt
writing top-level names to mojimoji.egg-info/top_level.txt
reading manifest file 'mojimoji.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'mojimoji.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-10.7-x86_64/egg
running install_lib
running build_ext
building 'mojimoji' extension
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/include -arch x86_64 -I/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/include -arch x86_64 -I/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/include/python3.7m -c mojimoji.cpp -o build/temp.macosx-10.7-x86_64-3.7/mojimoji.o -std=c++11 -D_hypot=hypot -stdlib=libc++
g++ -bundle -undefined dynamic_lookup -L/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/lib -arch x86_64 -L/Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.7-x86_64-3.7/mojimoji.o -o build/lib.macosx-10.7-x86_64-3.7/mojimoji.cpython-37m-darwin.so -stdlib=libc++ -mmacosx-version-min=10.9 -std=c++11 -D_hypot=hypot
creating build/bdist.macosx-10.7-x86_64
creating build/bdist.macosx-10.7-x86_64/egg
copying build/lib.macosx-10.7-x86_64-3.7/mojimoji.cpython-37m-darwin.so -> build/bdist.macosx-10.7-x86_64/egg
creating stub loader for mojimoji.cpython-37m-darwin.so
byte-compiling build/bdist.macosx-10.7-x86_64/egg/mojimoji.py to mojimoji.cpython-37.pyc
creating build/bdist.macosx-10.7-x86_64/egg/EGG-INFO
copying mojimoji.egg-info/PKG-INFO -> build/bdist.macosx-10.7-x86_64/egg/EGG-INFO
copying mojimoji.egg-info/SOURCES.txt -> build/bdist.macosx-10.7-x86_64/egg/EGG-INFO
copying mojimoji.egg-info/dependency_links.txt -> build/bdist.macosx-10.7-x86_64/egg/EGG-INFO
copying mojimoji.egg-info/top_level.txt -> build/bdist.macosx-10.7-x86_64/egg/EGG-INFO
writing build/bdist.macosx-10.7-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
__pycache__.mojimoji.cpython-37: module references __file__
creating dist
creating 'dist/mojimoji-0.0.9-py3.7-macosx-10.7-x86_64.egg' and adding 'build/bdist.macosx-10.7-x86_64/egg' to it
removing 'build/bdist.macosx-10.7-x86_64/egg' (and everything under it)
Processing mojimoji-0.0.9-py3.7-macosx-10.7-x86_64.egg
creating /Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/lib/python3.7/site-packages/mojimoji-0.0.9-py3.7-macosx-10.7-x86_64.egg
Extracting mojimoji-0.0.9-py3.7-macosx-10.7-x86_64.egg to /Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/lib/python3.7/site-packages
Adding mojimoji 0.0.9 to easy-install.pth file

Installed /Users/Jittsu/.pyenv/versions/anaconda3-5.3.1/lib/python3.7/site-packages/mojimoji-0.0.9-py3.7-macosx-10.7-x86_64.egg
Processing dependencies for mojimoji==0.0.9
Finished processing dependencies for mojimoji==0.0.9
```
と言う訳でインストールができ,mojimojiが使えるようになりました.
個人的にはかなり詰まったので参考になれば幸いです.

# 解決法まとめ
お急ぎの方,解決法がサクッと知りたい方向けです.

```console
$ cd $HOME
$ git clone https://github.com/studio-ousia/mojimoji
$ cd mojimoji
$ vim setup.py
```

```python:setup.py
# -*- coding: utf-8 -*-

import io
import sys
from setuptools import setup, Extension

if sys.platform.startswith('win'):
    compile_args = []
    link_args = []
else: # 以下編集箇所
    #compile_args = ['-std=c++11']
    #link_args = ['-std=c++11']
    compile_args=['-std=c++11', '-D_hypot=hypot', '-stdlib=libc++']
    link_args=['-stdlib=libc++', '-mmacosx-version-min=10.9','-std=c++11', '-D_hypot=hypot']
# 以上

setup(
    # 省略
)
```

```console
$ python setup.py install
```

以上で終了です.

# 参考
- https://qiita.com/nkmrtkhd/items/eac5828d031c72446e76
- https://github.com/cleardusk/3DDFA/issues/57