我有一个 python 项目,我想使用 Boost::Python 与一些 C++ 库交互。我想知道其他人如何在同一个项目中组织他们的 python/boost::python/C++ 代码。
我所说的组织是指文件/目录结构、构建过程等。
我有一个 python 项目,我想使用 Boost::Python 与一些 C++ 库交互。我想知道其他人如何在同一个项目中组织他们的 python/boost::python/C++ 代码。
我所说的组织是指文件/目录结构、构建过程等。
在下文中,pif 表示 Python 接口。首先,我有一个通用头文件,称为 conv_pif.hpp,它具有 Boost 头文件和 C++ Std 库头文件等。然后对于每个 boost python 模块,我都有一个 string_pif.cpp 形式的文件(这里对应于示例模块 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 对应于 C++ 向量到 Python 列表的(模板化)转换器。我在文件 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的body中。
******************************************
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 函数的标题。最后,我有一个 Scons 文件,它将所有内容链接在一起
******************************************
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)
在这种情况下,只有一个模块,所以 pif_files 只有 geno_pif.cpp。否则,我只会选择我想要的模块。嗯,也许在某处上传一个工作示例是最简单的。如果有人对更多细节感兴趣,我想我可以编辑这个?
问候,法希姆