C/C++与Python互相调用 (五)

2014-11-24 02:37:39 · 作者: · 浏览: 8
int 'this is Helloworld'
if __name__ == '__main__':
print 'this is main
PySys_SetArgv,设置参数;


PySys_SetPath,设置py文件路径;


如果用下面的代码:


[cpp]
PyObject* file = PyFile_FromString((char *) "mypy.py", (char*)"r");
FILE *fp = PyFile_AsFile(file);
PyRun_AnyFile(fp,"mypy.py");

PyObject* file = PyFile_FromString((char *) "mypy.py", (char*)"r");
FILE *fp = PyFile_AsFile(file);
PyRun_AnyFile(fp,"mypy.py");
来代替:


[cpp]
PyObject * pModule = NULL;
pModule =PyImport_ImportModule("mypy");

PyObject * pModule = NULL;
pModule =PyImport_ImportModule("mypy");
也可以执行mypy.py,但是PyRun_AnyFile会执行mypy.py中的__main__中的代码;


二、【Python调用C/C++

Python开发效率高,运行效率低。而c/c++恰恰相反。因此在python脚本中调用c/c++的库,对python进行扩展,是很有必要的。使用python api,http://www.python.org/doc/


1、

test.c


[cpp]
#include
void display() {
printf("This is Display Function\n");
}

#include
void display() {
printf("This is Display Function\n");
}
gcc -shared -fpic test.c -o libtestso1.so -I./include/python2.7


test.py


[python]
import ctypes
so = ctypes.CDLL("./libtestso1.so")
so.display()

import ctypes
so = ctypes.CDLL("./libtestso1.so")
so.display()

2、

testso2.cpp

[cpp]
#include
class TestLib{
public:
void display();
void display(int a);


};
void TestLib::display() {
std::cout<<"First display"< }


void TestLib::display(int a) {
std::cout<<"Second display"< }
extern "C" {
TestLib obj;
void display() {
obj.display();
}
void display_int() {
obj.display(2);
}
}

#include
class TestLib{
public:
void display();
void display(int a);


};
void TestLib::display() {
std::cout<<"First display"< }


void TestLib::display(int a) {
std::cout<<"Second display"< }
extern "C" {
TestLib obj;
void display() {
obj.display();
}
void display_int() {
obj.display(2);
}
}
g++ -shared -fpic testso2.cpp -o libtestso2.so -I./include/python2.7/


testso2.py

[python]
import ctypes
so = ctypes.CDLL("./libtestso2.so")
so.display()
so.display_int(1)

import ctypes
so = ctypes.CDLL("./libtestso2.so")
so.display()
so.display_int(1)

3、


testso.cpp


[cpp]
#include //包含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;
}

#include //包含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;
in