当前位置:首页 >> 设计

CBoost之Python一个简单的例子节能

2020-11-16 07:15:27  菏泽汽车网

假设我们有下面的C++ API需要暴露给Python: #include stringnamespace { // Avoid cluttering the global namespace. // A couple of simple C++ functions that we want to expose to Python. std::string greet() { retur此外n "hello, world"; } int square(int number) 因为这种令人发指的惨绝人寰行为早已超出了一个国家内政范畴{ return number * number; }}

这就是要暴露API给Python的getting_started1模块的C++源代码. #include boost/python/class_pnamespace python = boost::python;BOOST_PYTHON_MODULE_INIT(getting_started1){ try { // Create an object representing this extension module. python::module_builder this_module("getting_started1"); // Add regular functions to the module. this_f(greet, "greet"); this_f(square, "square"); } catch(...) { python::handle_exception(); // Deal with the exception for Python }}

成了! 如果我们生成这个共享库并把它放到Python的搜索路径中去, 我们就能在Python中访问这些C++函数了. import getting_started1 print getting_eet()hello, world number = 11 print number, '*', number, '=', getting_uare(number)11 * 11 = 121

Next: 导出类 Previous: 和其他系统的比较 Up: Top

阜阳白癜风医院在哪
延安有没有医院治疗白癜风
先声药业
友情链接
4