合肥网站建设技术支持,快速开发安卓app软件,网站开发项目经理招聘,鞋设计师之家官网一#xff1a;Python调用C语言场景
1#xff0c;已经写好的C语言代码#xff0c;不容易用Python实现#xff0c;想直接通过Python调用写好的C语言代码
2#xff0c;C比Python快#xff08;只是从语言层面#xff0c;不能绝对说C程序就是比Python快#xff09;
3Python调用C语言场景
1已经写好的C语言代码不容易用Python实现想直接通过Python调用写好的C语言代码
2C比Python快只是从语言层面不能绝对说C程序就是比Python快
3想直接调用C语言丰富的库
二Python的CTypes库
Python中调用C语言不止一种方法常见的有
1.SWIG编写一个额外的接口文件来作为SWIG(终端工具)的入口
2.通过CTypes调用
3.使用Python/C API方法
但是 CTypes是最简单上手的一种本文将用CTypes来演示如何在Python中调用C
可以先看看CTypes支持的类或者方法 import ctypes dir(ctypes) [ARRAY, ArgumentError, Array, BigEndianStructure, CDLL, CFUNCTYPE, DEFAULT_MODE, LibraryLoader, LittleEndianStructure, POINTER, PYFUNCTYPE, PyDLL, RTLD_GLOBAL, RTLD_LOCAL, SetPointerType, Structure, Union, _CFuncPtr, _FUNCFLAG_CDECL, _FUNCFLAG_PYTHONAPI, _FUNCFLAG_USE_ERRNO, _FUNCFLAG_USE_LASTERROR, _Pointer, _SimpleCData, __builtins__, __cached__, __doc__, __file__, __loader__, __name__, __package__, __path__, __spec__, __version__, _c_functype_cache, _calcsize, _cast, _cast_addr, _check_size, _ctypes_version, _dlopen, _endian, _memmove_addr, _memset_addr, _os, _pointer_type_cache, _reset_cache, _string_at, _string_at_addr, _sys, _wstring_at, _wstring_at_addr, addressof, alignment, byref, c_bool, c_buffer, c_byte, c_char, c_char_p, c_double, c_float, c_int, c_int16, c_int32, c_int64, c_int8, c_long, c_longdouble, c_longlong, c_short, c_size_t, c_ssize_t, c_ubyte, c_uint, c_uint16, c_uint32, c_uint64, c_uint8, c_ulong, c_ulonglong, c_ushort, c_void_p, c_voidp, c_wchar, c_wchar_p, cast, cdll, create_string_buffer, create_unicode_buffer, get_errno, memmove, memset, pointer, py_object, pydll, pythonapi, resize, set_errno, sizeof, string_at, wstring_at] 其中cdll类对于我们接下来调用C非常有用处我们通过help来查看cdll类可以看到可以通过LoadLibrary来加载C的库在Linux中是.so在windows中是.dll 三实战
1调用C语言的函数
首先我们在C语言中实现三个简单函数
#includestdio.hvoid cprintf()
{printf(Hello word!\n);return;
}int add(int a, int b)
{return ab;
}int cscanf()
{int a;int b;scanf(%d %d,a,b);return ab;
}
三个函数一个是有打印函数一个是有传参函数一个是有输入函数将C编译成动态库
gcc -fPIC -c ctest.c -o ctest.o
gcc -shared -o libctest.so ctest.o 然后我们在Python中调用这三个函数
from ctypes import *cLib cdll.LoadLibrary(./libctest.so)cLib.cprintf()a 1
b 2
sum cLib.add(a,b)
print(f{a}{b}{sum})cSum cLib.cscanf()
print(fcSum{cSum}) 运行 可以看到运行结果与我们想要的结果一致
2调用C语言返回值是指针的函数
C代码
#includestdio.h
#includestdlib.h
#includestring.h
#define uint8_t unsigned char
#define uint16_t unsigned shorttypedef struct TagMyStruct
{char name[10];uint8_t age;int score;
} MyStruct,*MyStructPointer;MyStructPointer cstruct_get_data_pointer() // 返回结构体指针
{MyStructPointer pt (MyStructPointer)malloc(sizeof(MyStructPointer));strcpy(pt-name, ftz);pt-age 18;pt-score 88;return pt;
}MyStruct cstruct_get_data_self() // 返回结构体
{return (MyStruct){csdn,100,99};
}
python调用
from ctypes import *cLib cdll.LoadLibrary(./cstruct.so)class MyStruct(Structure):_fields_ [(name, c_char*10),(age, c_ubyte),(score, c_int), ]cLib.cstruct_get_data_self.restype MyStruct #指定函数返回值结构体本身
dat cLib.cstruct_get_data_self()
print(get struct self from .so function)
print(name,dat.name)
print(age,dat.age)
print(score,dat.score)cLib.cstruct_get_data_pointer.restype POINTER(MyStruct) #指定函数返回值结构体指针
dat cLib.cstruct_get_data_pointer()
print(get struct point from .so function)
print(name,dat.contents.name)
print(age,dat.contents.age)
print(score,dat.contents.score)
运行结果
文章转载自: http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.mrxgm.cn.gov.cn.mrxgm.cn http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.xinxianzhi005.com.gov.cn.xinxianzhi005.com http://www.morning.ngpdk.cn.gov.cn.ngpdk.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.cgntj.cn.gov.cn.cgntj.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.lctrz.cn.gov.cn.lctrz.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.sltfk.cn.gov.cn.sltfk.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.drwpn.cn.gov.cn.drwpn.cn http://www.morning.qfths.cn.gov.cn.qfths.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.xnpml.cn.gov.cn.xnpml.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.jjtwh.cn.gov.cn.jjtwh.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.cwpny.cn.gov.cn.cwpny.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.bgqqr.cn.gov.cn.bgqqr.cn http://www.morning.seoqun.com.gov.cn.seoqun.com http://www.morning.xhgcr.cn.gov.cn.xhgcr.cn http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.nrrzw.cn.gov.cn.nrrzw.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.rqnzh.cn.gov.cn.rqnzh.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.gmysq.cn.gov.cn.gmysq.cn http://www.morning.qrmry.cn.gov.cn.qrmry.cn http://www.morning.yrccw.cn.gov.cn.yrccw.cn http://www.morning.sfnjr.cn.gov.cn.sfnjr.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.fqsxf.cn.gov.cn.fqsxf.cn http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.rkck.cn.gov.cn.rkck.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.wpkr.cn.gov.cn.wpkr.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.gwwky.cn.gov.cn.gwwky.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.bxqtq.cn.gov.cn.bxqtq.cn http://www.morning.pctsq.cn.gov.cn.pctsq.cn http://www.morning.gfqj.cn.gov.cn.gfqj.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.djmdk.cn.gov.cn.djmdk.cn http://www.morning.bkxnp.cn.gov.cn.bkxnp.cn http://www.morning.nckzt.cn.gov.cn.nckzt.cn http://www.morning.cknsx.cn.gov.cn.cknsx.cn http://www.morning.brwp.cn.gov.cn.brwp.cn http://www.morning.rnqyy.cn.gov.cn.rnqyy.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.xjqrn.cn.gov.cn.xjqrn.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.clpdm.cn.gov.cn.clpdm.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.qjlkp.cn.gov.cn.qjlkp.cn http://www.morning.bylzr.cn.gov.cn.bylzr.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn