BOOST_PYTHON_MODULE宏使用问题

wangjiepro 2011-09-07 10:02:10
利用boost.python的BOOST_PYTHON_MODULE宏可以方便的导出C++对象给Python使用,如下:

using namespace boost::python;

void hello()
{
std::cout<<"Hello from C :)"<<std::endl;
}

BOOST_PYTHON_MODULE(helloModule)
{
def("hello", hello);
}

文档上说这个宏会生成两个函数,inithelloModule()和init_module_helloModuel(),在python扩展时不需要自己调用inithelloModule(),不过在python嵌入的时候,要手动调用一下inithelloModule(),现在我用这个宏导出的时候,并没有生成inithelloModule(),只生成了init_module_helloModuel(),现在调用init_module_helloModuel()会出错,不知道是不是新版的boost中改了,我的boost版本是1_47_0
...全文
428 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Chandlerjou 2013-07-19
  • 打赏
  • 举报
回复
谢谢楼主分享经验,按照楼主的方法试了一下,但不知为何在链接的时候发生cannot open file 'boost_python-vc90-mt-gd-1_37.lib‘,不知道楼主有没有遇到过?若有,如何解决的呢?
wangjiepro 2011-09-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 iambic 的回复:]

楼主能贴下你说的“文档”链接吗?
[/Quote]

http://www.boost.org/doc/libs/1_47_0/libs/python/doc/v2/module.html#BOOST_PYTHON_MODULE-spec
wangjiepro 2011-09-09
  • 打赏
  • 举报
回复
wangjiepro 2011-09-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 angel_su 的回复:]

我是在python 2.x下试的,查了下PyInit_xxx这个是属于python 3.x
[/Quote]

恩,是,我用的Python是3.2.1的
iambic 2011-09-08
  • 打赏
  • 举报
回复
楼主能贴下你说的“文档”链接吗?
angel_su 2011-09-08
  • 打赏
  • 举报
回复
我是在python 2.x下试的,查了下PyInit_xxx这个是属于python 3.x
wangjiepro 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 angel_su 的回复:]

名字没变吧,试了下boost 1.47用inithelloModule是对的呀...
[/Quote]

是吗,我的为什么是错的?boost中代码是这样的

# define BOOST_PYTHON_MODULE BOOST_PYTHON_MODULE_INIT


# define BOOST_PYTHON_MODULE_INIT(name) \
void BOOST_PP_CAT(init_module_,name)(); \
extern "C" __declspec(dllexport) _BOOST_PYTHON_MODULE_INIT(name)


# define _BOOST_PYTHON_MODULE_INIT(name) \
PyObject* BOOST_PP_CAT(PyInit_, name)() \
{ \
static PyModuleDef_Base initial_m_base = { \
PyObject_HEAD_INIT(NULL) \
0, /* m_init */ \
0, /* m_index */ \
0 /* m_copy */ }; \
static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; \
\
static struct PyModuleDef moduledef = { \
initial_m_base, \
BOOST_PP_STRINGIZE(name), \
0, /* m_doc */ \
-1, /* m_size */ \
initial_methods, \
0, /* m_reload */ \
0, /* m_traverse */ \
0, /* m_clear */ \
0, /* m_free */ \
}; \
\
return boost::python::detail::init_module( \
moduledef, BOOST_PP_CAT(init_module_, name) ); \
} \
void BOOST_PP_CAT(init_module_, name)()


最后是用PyInit_连接的module name啊
angel_su 2011-09-08
  • 打赏
  • 举报
回复
名字没变吧,试了下boost 1.47用inithelloModule是对的呀...
wangjiepro 2011-09-08
  • 打赏
  • 举报
回复
问题解决了,原来BOOST_PYTHON_MODULE生成的函数名真的变了,现在是PyInit_xxx()和init_module_xxx()了,在python内嵌到C++中时,如果python需要使用C++的对象,要在Py_Initialize()前调用一下PyImport_AppendInittab( "xxx", &PyInit_xxx );

Now, inside main() and before the call to Py_Initialize() we want to call PyImport_AppendInittab( "CppMod", &initCppMod ); initCppMod is a function created by the BOOST_PYTHON_MODULE macro which is used to initialize the Python module CppMod. At this point, your embedded python script may call import CppMod and then access CppClass as a member of the module.
为了方便大家使用MinGW(GCC)+_boost.python,特意只做了三个dll,可以很方便地将c++代码转为python模块. libboost_python-mgw45-1_49.dll libboost_python-mgw45-d-1_49.dll python27.dll 这三个文件我已放在资源里面,大家可以下载. 下面说说使用方法: 第一步:编写一个hello_ext.cpp的c++源文件 #include <boost/python.hpp> // 第一行必须是#include <boost/python.hpp> // 否则会留下一点小问题 #include // 输出字符串 char const* greet() { return "hello, world"; } // 实现两个数字相加 int add(int x, int y) { return x + y; } // 打印vector的函数 void vprint() { std::vectormyvector; for (int i = 1; i <= 5; i++) { myvector.push_back(i); } std::vector::iterator it; std::cout << "myvector contains:"; for (it = myvector.begin(); it < myvector.end(); it++) { std::cout << " " << *it; } std::cout << std::endl; } // 定义python模块的接口文件 BOOST_PYTHON_MODULE(hello_ext) { // hello_ext为导出python模块的名字 using namespace boost::python; def("greet", greet); // 导出函数greet def("add", add); // 导出函数add def("vprint", vprint); // 导出函数vprint } 将上面的文件一定要保存为utf-8的格式(使用记事本在保存的时候就可以选择),不推荐Ansi格式! 然后就可以使用下面的命令编译python模块了: g++ -o hello_ext.pyd hello_ext.cpp -shared libboost_python-mgw45-1_49.dll python27.dll 也可以使用如下的命令,编译debug版本 g++ -o hello_ext.pyd hello_ext.cpp -shared libboost_python-mgw45-d-1_49.dll python27.dll 运行上面的命令之前,请确保hello_ext.cpp,libboost_python-mgw45-1_49.dll,libboost_python-mgw45-d-1_49.dll和 python27.dll在同一个目录. hello_ext.pyd就是python中能直接使用的动态链接库,windows一般以dll为后缀,而python只承认pyd文件. 下面来测试一下: import hello_ext print hello_ext.greet() print hello_ext.add(1,3) hello_ext.vprint() 输出为: hello, world 4 myvector contains: 1 2 3 4 5 看,成功了! ============================================================================= 使用g++编译常见的问题就是找不到文件<boost/python.hpp>和pyconfig.h等文件. 这些文件其实在boost的目录下面和C:\Python27\include目录中. 为了使用方便,将整个\boost_1_49_0\boost\目录复制到MinGw的include目录下面; 将C:\Python27\include目录下的文件全部复制到MinGw的include目录下面即可. 如果不想复制,也可以给g++设置-L参数 -LC:\boost\include\boost_1_49_0\ 和-LC:\Python27\include, 不过每次都这样,还是麻烦,不如复制一下彻底解决! 在发布hello_ext.pyd的时候,由于是动态链接库,所以不要忘了libboost_python-mgw45-1_49.dll, libboost_python-mgw45-d-1_49.dll和 python27.dll也要一起发布!

37,742

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧