您现在的位置是:主页 > news > html5网站开发案例视频/如何seo推广

html5网站开发案例视频/如何seo推广

admin2025/6/15 22:30:44news

简介html5网站开发案例视频,如何seo推广,做网站都需要服务器吗,深圳建设交易中心网站Python调用C 关键字:Python ctypes,Python调用dll,Python调用C函数。为了节省软件开发成本,软件开发人员希望能够缩短的软件的开 发时间,希望能够在短时间内开发出稳定的产品。Python 功能强大,简单易用,能…

html5网站开发案例视频,如何seo推广,做网站都需要服务器吗,深圳建设交易中心网站Python调用C 关键字:Python ctypes,Python调用dll,Python调用C函数。为了节省软件开发成本,软件开发人员希望能够缩短的软件的开 发时间,希望能够在短时间内开发出稳定的产品。Python 功能强大,简单易用,能…

Python调用C

关键字:Python ctypes,Python调用dll,Python调用C函数。为了节省软件开发成本,软件开发人员希望能够缩短的软件的开 发时间,希望能够在短时间内开发出稳定的产品。Python 功能强大,简单易用,能够快速开发应用软件。但是由于 Python 自身执行速度的局限性,对性能要求比较高的模块需要使用效率更高的程序语言进行开发,例如 C 语言,系统的其他模块运用 Python 进行快速开发,最后将 C 语言开发的模块与 Python 开发的模块进行整合。在此背景下,基于 Python 语言与 C 语言的各自特点,用 C 语言来扩展现有的 Python 程序,显得很有意义。

本文提供Python以链接库的形式调用C函数的实例。主要用到Python提供的ctypes实现。Python官方教程地址:Ctypes

  • Python调用C
    • 范例一Python 调用 C 语言 so
      • 第一步编写C函数testlibc
      • 第二步将C函数编译成链接库
      • 第三步在python中使用C链接库函数编写python代码testpy如下
      • 第四步just run it
    • 范例二参数类型为字符串即传指针
      • 第一步编写C函数testlib1c
      • 第二步将C函数编译成链接库
      • 第三步在python中使用C链接库函数编写python代码test1py如下
      • 第四步just run it
    • 范例三参数为指针数组传指针数组
      • 第一步编写C函数testlib2c
      • 第二步将C函数编译成链接库
      • 第三步在python中使用C链接库函数
      • 第四步just run it
    • 范例四 参数为结构体
      • 第一步编写C函数testlib3c
      • 第二步将C函数编译成链接库
      • 第三步在python中使用C链接库函数编写python代码test3py如下
      • 第四步just run it

范例一:Python 调用 C 语言 so

第一步:编写C函数,testlib.c

#include <stdio.h>
void myprint()
{printf("hello,www.cricode.com!n");
}
  • 1
  • 2
  • 3
  • 4
  • 5

第二步:将C函数编译成链接库

$ gcc -shared -Wl,-soname,testlib -o testlib.so -fPIC testlib.c

第三步:在python中使用C链接库函数,编写python代码test.py如下

import ctypes 
testlib = ctypes.CDLL('/path/to/testlib.so')
testlib.myprint()
  • 1
  • 2
  • 3

第四步:just run it

$ python test.py 
hello,www.cricode.com!

范例二:参数类型为字符串(即传指针)

当C函数中,参数类型为字符串时,使用ctypes提供的create_string_buffer来创建缓存区。

第一步:编写C函数testlib1.c

#include <string.h>
int reverse(char* str)
{int i,j,t;for(i=0,j=strlen(str) - 1;i<j;i++,j--){t = str[i];str[i] = str[j];str[j] = t;}return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

第二步:将C函数编译成链接库

$ gcc -shared -Wl,-soname,testlib1 -o testlib1.so -fPIC testlib1.c

第三步:在python中使用C链接库函数,编写python代码test1.py如下:

import ctypess0 = 'hello,www.cricode.com'
s1 = ctypes.create_string_buffer(s0)
testlib1 = ctypes.CDLL('./testlib1.so')
testlib1.reverse(s0)
print 's0 is: ',s0
print 's1 is: ',s1.value
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

第四步:just run it

$ python test1.py 
s0 is: moc.edocirc.www,olleh 
s1 is: hello,www.cricode.com

范例三:参数为指针、数组(传指针、数组)

第一步:编写C函数testlib2.c

void arrayFunc(int *sum,int arr[4])
{*sum = arr[0]+ arr[1]*2 + arr[2]*3 + arr[3]*4;
}
  • 1
  • 2
  • 3
  • 4

第二步:将C函数编译成链接库

$ gcc -shared -Wl,-soname,testlib2 -o testlib2.so -fPIC testlib2.c

第三步:在python中使用C链接库函数

import ctypesresult = ctypes.c_int()
array = ctypes.c_int*4
parray = array(ctypes.c_int(1),ctypes.c_int(2),ctypes.c_int(3),ctypes.c_int(4))testlib2 = ctypes.CDLL('./testlib2.so')
#testlib2.arrayFunc.argtypes=[ctypes.c_void_p,ctypes.c_int*4]
testlib2.arrayFunc(ctypes.pointer(result),parray)
print 'result is:',result.value
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

第四步:just run it

$ python test2.py 
result is: 17

范例四 参数为结构体

第一步:编写C函数testlib3.c

typedef struct{int x;int y;
}mystruct;
mystruct structFunc(mystruct a,mystruct b)
{mystruct s;s.x = a.x + b.x;s.y = a.y + b.y;return s;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

第二步:将C函数编译成链接库

$ gcc -shared -Wl,-soname,testlib3 -o testlib3.so -fPIC testlib3.c

第三步:在python中使用C链接库函数,编写python代码test3.py如下:

import ctypesclass mystruct(ctypes.Structure):_fields_ = [("x",ctypes.c_int),("y",ctypes.c_int)]a = mystruct(2,4)
b = mystruct(4,6)
testlib3 = ctypes.CDLL('./testlib3.so')
#srestype muste be setted to avoid segment fault
testlib3.structFunc.restype = mystruct
c = testlib3.structFunc(a,b)
print c.x,c.y
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

第四步:just run it

$ python test3.py 
6 10

  • 浅谈 Python 程序和 C 程序的整合 
    http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/
  • ctypes https://docs.python.org/2/library/ctypes.html
  • http://blog.csdn.net/golden1314521/article/details/44055523