Python

C in Python

ksg97031 2015. 10. 5. 18:24


C 라이브러리 파일 

Window - msvcrt.dll 

Linux - libc.so.6

Mac - /usr/lib/libc.dylib 

cdll.msvcrt / CDLL("libc.so.6") / CDLL('/usr/lib/libc.dylib') 


from ctypes import *

libc = CDLL("libc.so.6") # linux

libc.printf("THIS IS PRINTF")


ctypes typeC typePython type
c_bool_Boolbool (1)
c_charchar1-character string
c_wcharwchar_t1-character unicode string
c_bytecharint/long
c_ubyteunsigned charint/long
c_shortshortint/long
c_ushortunsigned shortint/long
c_intintint/long
c_uintunsigned intint/long
c_longlongint/long
c_ulongunsigned longint/long
c_longlong__int64 or long longint/long
c_ulonglongunsigned __int64 or unsigned long longint/long
c_floatfloatfloat
c_doubledoublefloat
c_longdoublelong doublefloat
c_char_pchar * (NUL terminated)string or None
c_wchar_pwchar_t * (NUL terminated)unicode or None
c_void_pvoid *int/long or None



struct ex_struct

{

int a;

unsinged int b;

};

class ex_struct(Structure):

_fields_ = [

("a", c_int),

("b", c_uint),

]


https://docs.python.org/2/library/ctypes.html