私はBoost :: Pythonを使っていくつかのC++ライブラリとインターフェイスしたいPythonプロジェクトを持っています。私は他の人が同じプロジェクト内でpython/boost :: python/C++コードを整理する方法について知りたいと思います。私は、ファイル/ディレクトリ構造の面で意味の組織によってpython/Boost Pythonプロジェクトの構成方法
、手順を構築するなど
私はBoost :: Pythonを使っていくつかのC++ライブラリとインターフェイスしたいPythonプロジェクトを持っています。私は他の人が同じプロジェクト内でpython/boost :: python/C++コードを整理する方法について知りたいと思います。私は、ファイル/ディレクトリ構造の面で意味の組織によってpython/Boost Pythonプロジェクトの構成方法
、手順を構築するなど
以下では、pifはPython InterFaceを示します。最初に、conv_pif.hppという一般的なヘッダーファイルがあります。これにはBoostヘッダーとC++ Stdライブラリヘッダーなどがあります。その後、各boost pythonモジュールのために、string_pif.cppという形式のファイル(ここではexampleモジュールgenocppに対応しています)があります。ここでstringはモジュールの名前にほぼ対応しています。
****************************************************************************************
geno_pif.cpp
****************************************************************************************
#include "conv_pif.hpp"
#include <boost/python.hpp>
#include "geno.hpp"
void export_cppvec_conv();
void export_geno()
{
boost::python::def("write_geno_table_affy6_to_file", write_geno_table_affy6_to_file);
}
BOOST_PYTHON_MODULE(genocpp)
{
export_geno();
export_cppvec_conv();
}
*****************************************************************************************
機能export_cppvec_convはPythonのリストに/ C++からベクター(鋳型)コンバータに相当します。私はファイルcppvec_conv_pif.cppに実際のコンバータを持っています。特に、これはテンプレートのインスタンシエーションを使用するexport_cppvec_convを定義しているので、geno_pif.cppにそれを含めずに取り除くことができます。例として、export_cppvec_convの内容は次のとおりです。ここで、cppvec_to_python_listおよびcppvec_from_python_listはcppvec_conv_pif.cppの本文に定義されています。
******************************************
cppvec_conv_pif.cpp (extract)
******************************************
void export_cppvec_conv()
{
boost::python::to_python_converter<vector<double>, cppvec_to_python_list<double> >();
cppvec_from_python_list<double>();
boost::python::to_python_converter<vector<int>, cppvec_to_python_list<int> >();
cppvec_from_python_list<int>();
boost::python::to_python_converter<vector<string>, cppvec_to_python_list<string> >();
cppvec_from_python_list<string>();
}
******************************************
genocppモジュールに必要な数のコンバータを追加できます。 もちろん、私はgeno.hppのgeno関数のヘッダーを持っています。 は最後に、私はこの場合、一緒に
******************************************
Sconstruct
******************************************
#!/usr/bin/python
import commands, glob, os
# Common file, for both executables and Python Interface
common_files = """geno print"""
def pyversion():
pystr = commands.getoutput('python -V')
version = pystr.split(' ')[1]
major, minor = version.split('.')[:2]
return major + '.' + minor
common_base = Split(common_files)
common = [f + ".cpp" for f in common_base]
# For Python interface only
pif_conv = Split("cppvec_conv cppmap_conv cppset_conv")
pif_conv_files = [t+"_pif.cpp" for t in pif_conv]
pif = Split("geno")
pif_files = [t+"_pif.cpp" for t in pif]
# Boost Python Environment
boost_python_env = Environment(
CPPPATH=["/usr/include/python"+pyversion(), "."],
CXXFLAGS='-ftemplate-depth-100 -fPIC -Wall -Werror -pedantic -pipe -O3 -ffast-math -march=opteron',
#CXXFLAGS='-ftemplate-depth-100 -fPIC -Wall -pedantic -O0 -g',
CPPDEFINES=['BOOST_PYTHON_DYNAMIC_LIB'],
LIBPATH=["/usr/lib/python"+pyversion()+"/config"],
LIBS=["python"+pyversion(), "m", "boost_python"],
SHLIBPREFIX="", #gets rid of lib prefix
SHOBJSUFFIX = ".bpo"
)
boost_python_env.SharedLibrary(target='genocpp', source = common + pif_conv_files + pif_files)
すべてをリンクsconsのファイルを持って、pif_filesのでちょうどgeno_pif.cppを持っている、唯一のモジュールがあります。それ以外の場合は、モジュールに必要なものだけを選択します。うーん、ちょうどどこかで実際のサンプルをアップロードするのが一番簡単かもしれません。誰かがもっと詳細に興味があれば、私はこれを編集できると思いますか?
よろしくお願いします。Faheem
boost.pythonは非常に高速なバインディングを作成しますが、これは問題のトピックではありません。 –