python调用 C/C++ 代码示例
2012-09-04 14:46
781 查看
Python开发效率高,运行效率低。而c/c++恰恰相反。因此在python脚本中调用c/c++的库,对python进行扩展,是很有必要的。使用python api,http://www.python.org/doc/,需要安装python-dev。
test.cpp文件如下
[cpp]
#include <usr/include/python2.7/Python.h> //包含python的头文件
// 1 c/cpp中的函数
int my_c_function(const char *arg) {
int n = system(arg);
return n;
}
// 2 python 包装
static PyObject * wrap_my_c_fun(PyObject *self, PyObject *args) {
const char * command;
int n;
if (!PyArg_ParseTuple(args, "s", &command))//这句是把python的变量args转换成c的变量command
return NULL;
n = my_c_function(command);//调用c的函数
return Py_BuildValue("i", n);//把c的返回值n转换成python的对象
}
// 3 方法列表
static PyMethodDef MyCppMethods[] = {
//MyCppFun1是python中注册的函数名,wrap_my_c_fun是函数指针
{ "MyCppFun1", wrap_my_c_fun, METH_VARARGS, "Execute a shell command." },
{ NULL, NULL, 0, NULL }
};
// 4 模块初始化方法
PyMODINIT_FUNC initMyCppModule(void) {
//初始模块,把MyCppMethods初始到MyCppModule中
PyObject *m = Py_InitModule("MyCppModule", MyCppMethods);
if (m == NULL)
return;
}
make:
g++ -shared -fpic test.cpp -o MyCppModule.so
编译完毕后,目录下会有一个MyCppModule.so文件
test.py文件如下
[python]
# -*- coding: utf-8 -*-
import MyCppModule
#导入python的模块(也就是c的模块,注意so文件名是MyCppModule
r = MyCppModule.MyCppFun1("ls -l")
print r
print "OK"
执行
lhb@localhost:~/maplib/clib/pyc/invokec$ python test.py
总计 20
-rwxr-xr-x 1 lhb lhb 45 2010-08-11 17:45 make
-rwxr-xr-x 1 lhb lhb 7361 2010-08-12 10:14 MyCppModule.so
-rw-r--r-- 1 lhb lhb 979 2010-08-11 17:45 test.cpp
-rw-r--r-- 1 lhb lhb 181 2010-08-11 17:45 test.py
0
OK
test.cpp文件如下
[cpp]
#include <usr/include/python2.7/Python.h> //包含python的头文件
// 1 c/cpp中的函数
int my_c_function(const char *arg) {
int n = system(arg);
return n;
}
// 2 python 包装
static PyObject * wrap_my_c_fun(PyObject *self, PyObject *args) {
const char * command;
int n;
if (!PyArg_ParseTuple(args, "s", &command))//这句是把python的变量args转换成c的变量command
return NULL;
n = my_c_function(command);//调用c的函数
return Py_BuildValue("i", n);//把c的返回值n转换成python的对象
}
// 3 方法列表
static PyMethodDef MyCppMethods[] = {
//MyCppFun1是python中注册的函数名,wrap_my_c_fun是函数指针
{ "MyCppFun1", wrap_my_c_fun, METH_VARARGS, "Execute a shell command." },
{ NULL, NULL, 0, NULL }
};
// 4 模块初始化方法
PyMODINIT_FUNC initMyCppModule(void) {
//初始模块,把MyCppMethods初始到MyCppModule中
PyObject *m = Py_InitModule("MyCppModule", MyCppMethods);
if (m == NULL)
return;
}
make:
g++ -shared -fpic test.cpp -o MyCppModule.so
编译完毕后,目录下会有一个MyCppModule.so文件
test.py文件如下
[python]
# -*- coding: utf-8 -*-
import MyCppModule
#导入python的模块(也就是c的模块,注意so文件名是MyCppModule
r = MyCppModule.MyCppFun1("ls -l")
print r
print "OK"
执行
lhb@localhost:~/maplib/clib/pyc/invokec$ python test.py
总计 20
-rwxr-xr-x 1 lhb lhb 45 2010-08-11 17:45 make
-rwxr-xr-x 1 lhb lhb 7361 2010-08-12 10:14 MyCppModule.so
-rw-r--r-- 1 lhb lhb 979 2010-08-11 17:45 test.cpp
-rw-r--r-- 1 lhb lhb 181 2010-08-11 17:45 test.py
0
OK
相关文章推荐
- 使用SWIG实现Python调用C/C++代码
- python调用Delphi写的Dll代码示例
- 【2013】实习收获4:关于python中利用ctypes调用c++代码的学习总结
- 利用Swig在python中调用C/C++代码
- c++调用python完整代码(开发环境vs2008)
- 【Cython】用Cython包装C++代码,提供给python调用
- 使用swig在python中调用c++代码
- C,C++代码中调用python脚本
- 如何让python调用C和C++代码
- 通过SWIG封装C/C++代码被Python调用
- python3调用R的示例代码
- 如何在C++代码中调用python代码(MinGW+Boost.Python)
- python调用C和C++代码(BOOST)
- C++用LuaIntf调用Lua代码示例
- 基于python的短信接口调用代码示例模板
- C语言代码中调用C++代码的方法示例
- 如何让python调用C和C++代码
- Windows C++程序调用包含TensorFlow代码的Python脚本出错问题的解决办法
- python调用C和C++代码
- C++用LuaIntf调用Lua代码示例