LoginSignup
4
1

More than 3 years have passed since last update.

WindowsでElixirのC++利用ライブラリビルドがコケるのを回避する

Last updated at Posted at 2019-01-02

fukuoka.exのpiacereです
ご覧いただいて、ありがとうございます:bow:

Windowsで、ElixirのC++利用ライブラリの一部(bcrypt_elixirなど)がビルドでコケるのを回避する手順を解説します

Visual Studioや「Build Tools for Visual Studio 2017」をインストールしたけど、うまくビルドできない、という方もご参考ください

内容が、面白かったり、気に入ったら、「いいね」よろしくお願いします :wink:

本コラムの検証環境、事前構築のコマンド

本コラムは、以下環境で検証しています

なお、「Phoenix」は、ElixirのWebフレームワークです

あと、以下コマンドでPhoenix PJを構築しています

mix phx.new materia_sample --no-ecto --no-webpack
cd materia_sample
mix compile

mix phx.gen.secretでコケる

bcrypt_elixirを使うライブラリの例として、fukuoka.exアドバイザーズのSIマイスター、@tuchiro さんが先月リリースしたOSS「Materia」を、「Materiaを導入してログイン認証してみる」に沿ってセットアップ中、「mix phx.gen.secret」を実施すると、bcrypt_elixirのビルドでコケます

ちなみに、Materiaは、Webユーザ認証/権限管理/トランザクション制御をElixirにアドインできるライブラリです

コケた際のエラーメッセージは、以下の通りです

mix phx.gen.secret
…(ビルドが続く)…
==> bcrypt_elixir

Microsoft(R) Program Maintenance Utility Version 14.14.26433.0
Copyright (C) Microsoft Corporation.  All rights reserved.

        del /Q /F priv
c:\piacere\code\materia_sample\deps\bcrypt_elixir\priv が見つかりませんでした。
        erl -eval "io:format(\"~s~n\", [lists:concat([\"ERTS_INCLUDE_PATH=\", code:root_dir(), \"/erts-\", erlang:system_info(version), \"/include\"])])" -s init stop -noshell > Makefile.auto.win
        nmake /                   /F Makefile.win priv\bcrypt_nif.dll

Microsoft(R) Program Maintenance Utility Version 14.14.26433.0
Copyright (C) Microsoft Corporation.  All rights reserved.

        if NOT EXIST "priv" mkdir "priv"
        cl /O2 /EHsc /I"c_src" /I"c:/Tools/erl9.0/erts-9.0/include" /LD /MD /Fepriv\bcrypt_nif.dll  c_src\bcrypt_nif.c c_src\blowfish.c
Microsoft(R) C/C++ Optimizing Compiler Version 19.14.26433 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

bcrypt_nif.c
c_src\bcrypt_nif.c(52): fatal error C1083: include ファイルを開けません。'stdio.h':No such file or directory
blowfish.c
c:\piacere\code\materia_sample\deps\bcrypt_elixir\c_src\blf.h(37): fatal error C1083: include ファイルを開けません。'stdint.h':No such file or directory
コードを生成中...
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\Hostx64\x64\cl.EXE"' : リターン コード '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\Hostx64\x64\nmake.EXE"' : リターン コード '0x2'
Stop.
could not compile dependency :bcrypt_elixir, "mix compile" failed. You can recompile this dependency with "mix deps.compile bcrypt_elixir", update it with "mix deps.update bcrypt_elixir" or clean it with "mix deps.clean bcrypt_elixir"
==> materia_sample
** (Mix) Could not compile with "nmake" (exit status: 2).
One option is to install a recent version of
[Visual C++ Build Tools](http://landinghub.visualstudio.com/visual-cpp-build-tools)
either manually or using [Chocolatey](https://chocolatey.org/) -
`choco install VisualCppBuildTools`.

After installing Visual C++ Build Tools, look in the "Program Files (x86)"
directory and search for "Microsoft Visual Studio". Note down the full path
of the folder with the highest version number. Open the "run" command and
type in the following command (make sure that the path and version number
are correct):

    cmd /K "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64

This should open up a command prompt with the necessary environment variables
set, and from which you will be able to run the "mix compile", "mix deps.compile",
and "mix test" commands.

エラーメッセージを見ると、「stdio.h」が見つからず、nmakeが失敗していると出ていて、その下に、対策コマンドが記載されていますが、情報が古く、「Build Tools for Visual Studio 2017」では動かないため、「Build Tools for Visual Studio 2017」に合わせた設定が必要です

Visual Studioのビルド設定を行い、ビルドを通す

以下コマンドを実行すると、C++のヘッダーファイルへのパスが通り、ビルドが通るようになります

なお、「Build Tools for Visual Studio 2017」のインストール位置を変更している場合は、下記の「~\2017」より手前のフォルダパスを適宜修正してください

64ビットWindowsの場合
"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x64
32ビットWindowsの場合
"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x86

再度、ビルドを行います

mix phx.gen.secret
…(ビルドが続く)…
==> bcrypt_elixir

Microsoft(R) Program Maintenance Utility Version 14.16.27025.1
Copyright (C) Microsoft Corporation.  All rights reserved.

        del /Q /F priv
        erl -eval "io:format(\"~s~n\", [lists:concat([\"ERTS_INCLUDE_PATH=\", code:root_dir(), \"/erts-\", erlang:system_info(version), \"/include\"])])" -s init stop -noshell > Makefile.auto.win
        nmake /                   /F Makefile.win priv\bcrypt_nif.dll

Microsoft(R) Program Maintenance Utility Version 14.16.27025.1
Copyright (C) Microsoft Corporation.  All rights reserved.

        if NOT EXIST "priv" mkdir "priv"
        cl /O2 /EHsc /I"c_src" /I"c:/Tools/erl9.0/erts-9.0/include" /LD /MD /Fepriv\bcrypt_nif.dll  c_src\bcrypt_nif.c c_src\blowfish.c
Microsoft(R) C/C++ Optimizing Compiler Version 19.16.27025.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

bcrypt_nif.c
blowfish.c
コードを生成中...
Microsoft (R) Incremental Linker Version 14.16.27025.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/dll
/implib:priv\bcrypt_nif.lib
/out:priv\bcrypt_nif.dll
bcrypt_nif.obj
blowfish.obj
   ライブラリ priv\bcrypt_nif.lib とオブジェクト priv\bcrypt_nif.exp を作成中
Compiling 3 files (.ex)
Generated bcrypt_elixir app
…(ビルドが続く)…

うまくビルドできました

p.s.「いいね」よろしくお願いします

よろしければ、ページ左上の image.pngimage.png のクリックをお願いしますー:bow:

4
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1