BoostのPython用ライブラリを使いたかったのでコンパイルする
参考文献。
Boost 最新版をソースコードからビルドし、インストール(MSVC ビルドツールを使用)(Windows 上)
Visual Studio 2019の x64 Native Tools Command Prompt for VS 2019
を使って、
cd (解凍したディレクトリ) bootstrap.bat mkdir stage b2.exe install -j2 --prefix=stage toolset=msvc-14.2 link=static,shared address-model=64 --with-python
を行う。 -j2
の1は1並列で行うの意味。Visual Studio側では、Visual Studio 2019は v142
となっているため、msvc-16.0 とはしない。
次に、Visual Studio 2019で次のサイトで書いてることをやってみる。
Boost.Python Tutorial - 1.70.0
Visual Studio 2019で、プロジェクト名「hello_ext」を「C++ 空のプロジェクト」で作成する。
- Pathを正しく通す
- 構成プロパティ-> 全般 -> 構成の種類 は ダイナミックライブラリ (.dll) にする
- 構成プロパティ-> 全般 -> ターゲットの拡張子 は
.pyd
にする
hello_ext.pyd
が x64のRelease内に出力されるので、それをpythonを実行するディレクトリにコピーし、そこでpythonを起動する
>>> import hello_ext >>> print hello_ext.greet() hello, world
これで動くはずである。
#include <boost/python.hpp> BOOST_PYTHON_MODULE(hello_ext) { using namespace boost::python; def("greet", greet); }
ここの、 BOOST_PYTHON_MODULE
内 今回の例では、hello_ext はプロジェクト名 (=出力する .pyd
名)と同じでなければならない。
boost::python(1) classと関数の使用方法(PythonからC++の呼び出し) - のんびりしているエンジニアの日記