"Go" Python calls C function

Source: Internet
Author: User

Original link:

Python calls C function-fast class network
Http://www.cricode.com/359.html

Keyword: Python ctypes,python call Dll,python call C function
To save software development costs, software developers want to shorten the development time of the software, hoping to develop a stable product in a short period of time. Python is powerful, easy-to-use, and fast to develop application software. However, due to the limitations of Python's own execution speed, modules with higher performance requirements need to be developed using more efficient programming languages, such as C, where other modules of the system are developed quickly using Python, and the modules developed by the C language are integrated with those developed by Python. In this context, based on the Python language and C language of their respective characteristics, C language to expand the existing Python program, it seems very meaningful.
This article provides python to invoke an instance of the C function in the form of a link library. Mainly used in Python-provided ctypes implementations. Official Python tutorial Address: ctypes

Example one: Python calls C language so
The first step: Writing C functions, TESTLIB.C

#include <stdio.h>
void Myprint ()
{
printf ("Hello,www.cricode.com!n");
}
Step two: Compile the C function into a linked library

$ gcc-shared-wl,-soname,testlib-o Testlib.so-fpic TESTLIB.C
If you are in Mac OS X,
$ gcc-shared-wl,-install_name,testlib.so-o Testlib.so-fpic TESTLIB.C
Step three: Using C-link library functions in python, write Python code test.py as follows

Import cTYPES

Testlib = cTYPES. Cdll ('/path/to/testlib.so ')
Testlib.myprint ()

Fourth step: just run it

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

Example two: The parameter type is a string (that is, the pointer is passed)
When the parameter type is a string in the C function, use the Create_string_buffer provided by cTYPES to create the buffer.

The first step: Writing the C function 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;
}
Step two: Compile the C function into a linked library

$ gcc-shared-wl,-soname,testlib1-o Testlib1.so-fpic testlib1.c

Step three: Using C-link library functions in python, write Python code test1.py as follows:

Import cTYPES

S0 = ' 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
Fourth step: just run it

$ python test1.py
S0 Is:moc.edocirc.www,olleh
S1 is:hello,www.cricode.com

Example three: parameters are pointers, arrays (pointers, arrays)
The first step: Writing the C function testlib2.c

void Arrayfunc (int *sum,int arr[4])
{
*sum = arr[0]+ arr[1]*2 + arr[2]*3 + arr[3]*4;
}
Step two: Compile the C function into a linked library

$ gcc-shared-wl,-soname,testlib2-o Testlib2.so-fpic testlib2.c

Step three: Use C-link library functions in Python,

Import cTYPES

result = 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
Fourth step: just run it

$ python test2.py
Result Is:17

The example four parameters are structural bodies
The first step: Writing the C function testlib3.c

Write the Python code test2.py as follows:

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;
}

Step two: Compile the C function into a linked library

$ gcc-shared-wl,-soname,testlib3-o Testlib3.so-fpic testlib3.c

Step three: Using C-link library functions in python, write Python code test3.py as follows:

Import cTYPES

Class 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
Fourth step: just run it

$ python test3.py
6 10
Example five use of the callback function
What is a callback function?

A callback function is defined by yourself, but you never call it a generic term for a class of functions.

"That is to say, you are a living Lei Feng, you are making a wedding dress for others"

Continue ....

Go Python calls the C function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.