当前位置:首页 » 编程语言 » c移植到python

c移植到python

发布时间: 2023-08-30 07:13:01

‘壹’ 求助 关于c程序中嵌入python的问题

嵌入
与python的扩展相对,嵌入是把Python解释器包装到C的程序中。这样做可以给大型的,单一的,要求严格的,私有的并且(或者)极其重要的应用程序内嵌Python解释器的能力。一旦内嵌了Python,世界完全不一样了。

C调用python中的函数:
hw.py:

#coding=utf8

def hw_hs(canshu):
return canshu

if __name__ == "__main__":
ccss = "I am hw"
print hw_hs(ccss)

helloWorld.py:

#coding=utf8
import hw

def hello():
ccss = "I am helloWorld"
return hw.hw_hs(ccss)

if __name__ == "__main__":
print hello()

testcpypy.c:

//#include "testcpypy.h"
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
//初始化Python
Py_Initialize();
if (!Py_IsInitialized()) {
printf("Py_Initialize");
getchar();
return -1;
}

//执行python语句
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");

PyObject *pMole = NULL;
PyObject *pFunc = NULL;
PyObject *reslt =NULL;

//载入python模块
if(!(pMole = PyImport_ImportMole("helloWorld"))) {
printf("PyImport_ImportMole");
getchar();
return -1;
}

//查找函数
pFunc = PyObject_GetAttrString(pMole, "hello");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [hello]");
getchar();
return -1;
}

//调用python中的函数
reslt = (PyObject*)PyEval_CallObject(pFunc, NULL);
//printf("function return value : %d\r\n", PyInt_AsLong(reslt));

//将python返回的对象转换为C的字符串
char *resltc=NULL;
int res;
res = PyArg_Parse(reslt, "s", &resltc);
if (!res) {
printf("PyArg_Parse");
getchar();
return -1;
}
printf("resltc is %s", resltc);
getchar();

//释放内存
Py_DECREF(reslt);
Py_DECREF(pFunc);
Py_DECREF(pMole);

//关闭python
Py_Finalize();

return 0;
}

编译
gcc -o testcpypy testcpypy.c -IC:\Python27\include -LC:\Python27\libs -lpython27 ---C:\Python27为python安装目录
或:
gcc -c testcpypy.c -IC:\Python27\include
gcc -o testcpypy.exe testcpypy.o -LC:\Python27\libs -lpython27
执行结果:

带参数的情况:

#include "callpydll.h"
#include "Python.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

int callhello(char *instr, char *outstr)
{

PyObject *pMole = NULL;
PyObject *pFunc = NULL;
PyObject *reslt = NULL;
PyObject *pParm = NULL;

char *resltc = NULL;
int resltn;
int res;

char *helloWorld = "TestIM_ProtocBuf";

char *im_account = "aaaa";
char *auth_code = "aaaa";
char *im_uid = "aaaa";
char *proxy_topic = "";

//初始化Python
Py_Initialize();
if (!Py_IsInitialized()) {
printf("Py_Initialize");
getchar();
return -1;
}

//执行python语句
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");

//载入python模块
if(!(pMole = PyImport_ImportMole(helloWorld))) {
printf("PyImport_ImportMole");
getchar();
return -2;
}

//查找函数
pFunc = PyObject_GetAttrString(pMole, "login_proxy_body_serialize");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [hello]");
getchar();
return -3;
}

//参数转换C --> python, 参数必须是元组(一个参数也是,否则会失败!!!坑啊)
pParm = Py_BuildValue("(ssss)", im_account, auth_code, im_uid, proxy_topic);

//调用python中的函数
reslt = (PyObject*)PyEval_CallObject(pFunc, pParm);

//将python返回的对象转换为C的字符串
res = PyArg_ParseTuple(reslt, "si", &resltc, &resltn);
if (!res) {
printf("PyArg_Parse");
getchar();
return -4;
}

printf("resltn is %d", resltn);
memcpy(outstr, resltc, strlen(resltc)+1);

//释放内存
Py_DECREF(reslt);
Py_DECREF(pFunc);
Py_DECREF(pMole);
Py_DECREF(pParm);

//关闭python
Py_Finalize();

return 0;
}

int main() {
int i;
char *dais = "iammain";
char res[10240];
memset(res,'\0',sizeof(res));

i = callhello(dais, res);
if(0 != i) {
printf("Notify:error");
getchar();
return -1;
}
printf("result is %s", res);
getchar();
return 0;
}

‘贰’ C语言怎么转化成python

要将C语言转化为python,前提:
1
对C语言语法即python编程均熟练掌握;
2
算法相关有一定了解;
3
至少可以看懂要转换的C语言程序。
由于C语言的语句,与python没有对应关系,所以只能按照如下方式转换:
1
读懂C语言代码实现功能,可以以函数为单位;
2
按照功能,依照python方式实现相同功能。

‘叁’ 怎么把这个C语言转换成python

C语言不能转化为python,它们之间没有之间联系,只能说算法是可以转化实现的。

‘肆’ c可以调用python吗

可以的。

C中内嵌Python
新建立一个工程,首先需要将工作目录设置到Python-3.1.1PCbuild中,以获取到动态库,至于静态库的包含,Include目录的指定,那自然也是少不了的。文件中需要包含Python.h文件,这也是必须的。
接口中
Py_Initialize();
Py_Finalize();

其他的根据需求,再引入相应的python builder 即可

‘伍’ c如何调用python程序

C语言如何调用python,相关步骤如下:

首先,C语言中调用python,要使用头文件Python.h。

2、接着,定义一个调用python的函数。

相关推荐:《Python教程》

3、函数中,设置python库的路径。

4、然后,初始化python。

5、运行一个python代码,输出How are you。

6、最后,释放python。

热点内容
linuxdateshell 发布:2025-03-14 18:04:13 浏览:983
mysql数据库迁移方案 发布:2025-03-14 18:04:12 浏览:388
如何通过中转服务器访问外网 发布:2025-03-14 18:03:31 浏览:15
linux挂载点与分区 发布:2025-03-14 17:58:10 浏览:318
redmine数据库 发布:2025-03-14 17:46:46 浏览:656
c语言编辑器软件 发布:2025-03-14 17:46:39 浏览:862
java远程服务器文件 发布:2025-03-14 17:40:00 浏览:224
小米手机怎么关闭脚本工具 发布:2025-03-14 17:33:46 浏览:118
我的世界正版服务器怎么导出 发布:2025-03-14 17:23:31 浏览:621
php和aspnet 发布:2025-03-14 17:19:05 浏览:712