2007年01月16日 星期二 20:47
用boost.python库能解决了 可惜我的程序都是用C++ Builder 写的 boost.python 不支持 C++ Builder 分享下解决的方法: #includepython.hpp> #pragma comment(lib, "boost_python.lib") using namespace boost::python; class CppClass { public: int a; CppClass():a(0){} int getNum() { return a; } void setNum(int num) { a = num; } }; int main( int argc, char ** argv ) { try { Py_Initialize(); object main_module(( handle<>(borrowed(PyImport_AddModule("__main__"))))); object main_namespace = main_module.attr("__dict__"); main_namespace["CppClass"] = class_ i>>Date: Mon, 15 Jan 2007 19:07:17 +0800 >From: " 玄利磊 " <kency在zctt.com.cn> >Subject: [python-chinese] > 把Python嵌入到C++应用程序中__参数传递问题 >To: "python-chinese" <python-chinese在lists.python.cn> >Cc: xuanll <xuanll在zctt.com.cn> >Message-ID: <200701151906513433193在zctt.com.cn> >Content-Type: text/plain; charset="gb2312" > >用C++ 写的程序 现需要调用 python脚本进行数据处理 >使用下面函数: >pargs = Py_BuildValue("(s)", cstr); >PyEval_CallObject(pfunc, pargs); > >作为参数传递给 python 的只是些 string integer 等等 > >现在需要 把一个 类 或 指针 传递给 python 该如何做啊? > >谢谢!!! >("CppClass") .def("getNum",&CppClass::getNum) .def("setNum",&CppClass::setNum); CppClass cpp; cpp.setNum(1); main_namespace["pycpp"] = ptr(&cpp); handle<> ignored(( PyRun_String( "print pycpp.getNum() \n", Py_file_input, main_namespace.ptr(), main_namespace.ptr() ) )); } catch( error_already_set ) { PyErr_Print(); } return 0; } >Message: 7 <
2007年01月16日 星期二 21:35
ÕâÊÇÒ»¸öÀÏÎÊÌâÁË£¬Boost::Python On WindowsÖ§³ÖVCÖ®ÍâµÄ±àÒëÆ÷Â𣿵±È»Ö§³Ö£¬ Ç°ÌáÊÇÄãÒªÔÚ±àÒëBoostʱÌṩ¶ÔÓ¦±àÒëÆ÷±àÒëµÄPythonXX.lib¾²Ì¬Á´½Ó¿â£¬Ð°汾µÄPythonÌṩÁËMingw/CygnuËù ÐèµÄPythonXX.a¡£µ«ÊÇBCBµÄ¿ÖÅÂÄãÒª×Ô¼º±àÒë¡£ 2007/1/16, ÐþÀûÀÚ <kency在zctt.com.cn>: > > ÓÃboost.python¿âÄܽâ¾öÁË > ¿ÉϧÎҵijÌÐò¶¼ÊÇÓÃC++ Builder дµÄ > boost.python ²»Ö§³Ö C++ Builder > > ·ÖÏíϽâ¾öµÄ·½·¨: > > #includepython.hpp> < i>> #pragma comment(lib, "boost_python.lib") > > using namespace boost::python; > > class CppClass { > public: > int a; > CppClass():a(0){} > int getNum() > { > return a; > } > void setNum(int num) > { > a = num; > } > > }; > > > > int main( int argc, char ** argv ) > { > try > { > Py_Initialize(); > > object main_module(( > handle(borrowed(PyImport_AddModule("__main__"))))); > > object main_namespace = main_module.attr("__dict__"); > > main_namespace["CppClass"] = class_("CppClass") > .def("getNum",&CppClass;::getNum) > > .def("setNum",&CppClass;::setNum); > > CppClass cpp; > cpp.setNum(1); > > main_namespace["pycpp"] = ptr(&cpp;); > > > > handle ignored(( PyRun_String( "print pycpp.getNum() \n", > Py_file_input, > main_namespace.ptr(), > main_namespace.ptr() ) )); > > } > catch( error_already_set ) > { > PyErr_Print(); > } > return 0; > } > > > > >Message: 7 > >Date: Mon, 15 Jan 2007 19:07:17 +0800 > >From: " ÐþÀûÀÚ " <kency在zctt.com.cn> > >Subject: [python-chinese] > > °ÑPythonǶÈëµ½C++Ó¦ÓóÌÐòÖÐ__²ÎÊý´«µÝÎÊÌâ > >To: "python-chinese" <python-chinese在lists.python.cn> > >Cc: xuanll <xuanll在zctt.com.cn> > >Message-ID: <200701151906513433193在zctt.com.cn> > >Content-Type: text/plain; charset="gb2312" > > > >ÓÃC++ дµÄ³ÌÐò ÏÖÐèÒªµ÷Óà python½Å±¾½øÐÐÊý¾Ý´¦Àí > >ʹÓÃÏÂÃ溯Êý: > >pargs = Py_BuildValue("(s)", cstr); > >PyEval_CallObject(pfunc, pargs); > > > >×÷Ϊ²ÎÊý´«µÝ¸ø python µÄÖ»ÊÇЩ string integer µÈµÈ > > > >ÏÖÔÚÐèÒª °ÑÒ»¸ö Àà »ò Ö¸Õë ´«µÝ¸ø python ¸ÃÈçºÎ×ö°¡? > > > >лл!!! > > > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- Blog°á¼ÒÁË ÁõöÎ March.Liu -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070116/7b6b6f0c/attachment.htm
2007年01月17日 星期三 21:07
ÕâÊÇÒ»¸ö¾µäµÄÀÏÎÊÌâÁË£¬Boost::Python On WindowsÖ§³ÖVCÖ®ÍâµÄ±àÒëÆ÷Â𣿵±È»Ö§³Ö£¬ Ç°ÌáÊÇÄãÒªÔÚ±àÒëBoostʱÌṩ¶ÔÓ¦±àÒëÆ÷±àÒëµÄPythonXXX.lib¾²Ì¬¿â£º£© 2007/1/16, ÐþÀûÀÚ <kency在zctt.com.cn>: > > ÓÃboost.python¿âÄܽâ¾öÁË > ¿ÉϧÎҵijÌÐò¶¼ÊÇÓÃC++ Builder дµÄ > boost.python ²»Ö§³Ö C++ Builder > > ·ÖÏíϽâ¾öµÄ·½·¨: > > #includepython.hpp> < i>> #pragma comment(lib, "boost_python.lib") > > using namespace boost::python; > > class CppClass { > public: > int a; > CppClass():a(0){} > int getNum() > { > return a; > } > void setNum(int num) > { > a = num; > } > > }; > > > > int main( int argc, char ** argv ) > { > try > { > Py_Initialize(); > > object main_module(( > handle(borrowed(PyImport_AddModule("__main__"))))); > > object main_namespace = main_module.attr("__dict__"); > > main_namespace["CppClass"] = class_("CppClass") > .def("getNum",&CppClass;::getNum) > > .def("setNum",&CppClass;::setNum); > > CppClass cpp; > cpp.setNum(1); > > main_namespace["pycpp"] = ptr(&cpp;); > > > > handle ignored(( PyRun_String( "print pycpp.getNum() \n", > Py_file_input, > main_namespace.ptr(), > main_namespace.ptr() ) )); > > } > catch( error_already_set ) > { > PyErr_Print(); > } > return 0; > } > > > > >Message: 7 > >Date: Mon, 15 Jan 2007 19:07:17 +0800 > >From: " ÐþÀûÀÚ " < kency在zctt.com.cn> > >Subject: [python-chinese] > > °ÑPythonǶÈëµ½C++Ó¦ÓóÌÐòÖÐ__²ÎÊý´«µÝÎÊÌâ > >To: "python-chinese" <python-chinese在lists.python.cn > > >Cc: xuanll <xuanll在zctt.com.cn> > >Message-ID: <200701151906513433193在zctt.com.cn> > >Content-Type: text/plain; charset="gb2312" > > > >ÓÃC++ дµÄ³ÌÐò ÏÖÐèÒªµ÷Óà python½Å±¾½øÐÐÊý¾Ý´¦Àí > >ʹÓÃÏÂÃ溯Êý: > >pargs = Py_BuildValue("(s)", cstr); > >PyEval_CallObject(pfunc, pargs); > > > >×÷Ϊ²ÎÊý´«µÝ¸ø python µÄÖ»ÊÇЩ string integer µÈµÈ > > > >ÏÖÔÚÐèÒª °ÑÒ»¸ö Àà »ò Ö¸Õë ´«µÝ¸ø python ¸ÃÈçºÎ×ö°¡? > > > >лл!!! > > > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- Blog°á¼ÒÁË ÁõöÎ March.Liu -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070117/cf8a3ddc/attachment.html
Zeuux © 2025
京ICP备05028076号