#include <Python.h>
int Add(int x, int y)
{
return x + y;
}
int Del(int x, int y)
{
return x - y;
}
PyObject* WrappAdd(PyObject* self, PyObject* args)
{
int x, y;
if (!PyArg_ParseTuple(args, "ii", &x, &y))
{
return NULL;
}
return Py_BuildValue("i", Add(x, y));
}
PyObject* WrappDel(PyObject* self, PyObject* args)
{
int x, y;
if (!PyArg_ParseTuple(args, "ii", &x, &y))
{
return NULL;
}
return Py_BuildValue("i", Del(x, y));
}
static PyMethodDef test_methods[] = {
{"Add", WrappAdd, METH_VARARGS, "something"},
{"Del", WrappDel, METH_VARARGS, "something"},
{NULL, NULL}
};
extern "C"
void inittest1()
{
Py_InitModule("test1", test_methods);
}
g++ -fPIC -shared test1.cpp -I/usr/include/python2.6 -o test1.so
>>> import test1 >>> test1.Add(1,2) 3
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
int Add(const int x, const int y)
{
return x + y;
}
int Del(const int x, const int y)
{
return x - y;
}
BOOST_PYTHON_MODULE(test2)
{
def("Add", Add);
def("Del", Del);
}
g++ test2.cpp -fPIC -shared -o test2.so -I/usr/include/python2.6 -I/usr/local/include -L/usr/local/lib -lboost_python
#!/usr/bin/env python
from distutils.core import setup
from distutils.extension import Extension
setup(name="PackageName",
ext_modules=[
Extension("test2", ["test2.cpp"],
libraries = ["boost_python"])
])
python setup.py build
>>> import test2 >>> test2.Add(1,2) 3 >>> test2.Del(1,2) -1
#include <boost/python.hpp>
using namespace boost::python;
class Test
{
public:
int Add(const int x, const int y)
{
return x + y;
}
int Del(const int x, const int y)
{
return x - y;
}
};
BOOST_PYTHON_MODULE(test3)
{
class_<Test>("Test")
.def("Add", &Test::Add)
.def("Del", &Test::Del);
}
class_<Test>("Test").def("Add", &Test::Add);
class_<Test>("Test").def("Del", &Test::Del);
g++ test3.cpp -fPIC -shared -o test3.so -I/usr/include/python2.6 -I/usr/local/include/boost -L/usr/local/lib -lboost_python
>>> import test3 >>> test = test3.Test() >>> test.Add(1,2) 3 >>> test.Del(1,2) -1
#include <boost/python.hpp>
using namespace boost::python;
class Test
{
public:
int Add(const int x, const int y, const int z = 100)
{
return x + y + z;
}
};
int Del(const int x, const int y, const int z = 100)
{
return x - y - z;
}
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Add_member_overloads, Add, 2, 3)
BOOST_PYTHON_FUNCTION_OVERLOADS(Del_overloads, Del, 2, 3)
BOOST_PYTHON_MODULE(test4)
{
class_<Test>("Test")
.def("Add", &Test::Add, Add_member_overloads(args("x", "y", "z"), "something"));
def("Del", Del, Del_overloads(args("x", "y", "z"), "something"));
}
g++ test4.cpp -fPIC -shared -o test4.so -I/usr/include/python2.6 -I/usr/local/include/boost -L/usr/local/lib -lboost_python
>>> import test4 >>> test = test4.Test() >>> print test.Add(1,2) 103 >>> print test.Add(1,2,z=3) 6 >>> print test4.Del(1,2) -1 >>> print test4.Del(1,2,z=3) -1
def Square(list_a)
{
return [x * x for x in list_a]
}
#include <boost/python.hpp>
boost::python::list Square(boost::python::list& data)
{
boost::python::list ret;
for (int i = 0; i < len(data); ++i)
{
ret.append(data[i] * data[i]);
}
return ret;
}
BOOST_PYTHON_MODULE(test5)
{
def("Square", Square);
}
g++ test5.cpp -fPIC -shared -o test5.so -I/usr/include/python2.6 -I/usr/local/include/boost -L/usr/local/lib -lboost_python
>>> import test5 >>> test5.Square([1,2,3]) [1, 4, 9]
boost::python::tuple(int a, int b, int c)
{
return boost::python::make_tuple(a, b, c);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有