Python calls the C language

Source: Internet
Author: User

The cTYPES module in Python is probably the simplest of the python calls to the C method. The cTYPES module provides data types and functions that are compatible with the C language to load DLL files, so there is no need to make any modifications to the source file at the time of the call. And that is how simplicity of this approach is laid.

Examples such as the following

The C code that implements the sum of two numbers is saved asadd.c

//sample C file to add 2 numbers-int and floats#include<stdio.h>intAdd_int (int,int);floatAdd_float (float,float);intAdd_int (intNUM1,intnum2) {    returnNUM1 +num2;}floatAdd_float (floatNUM1,floatnum2) {    returnNUM1 +num2;}

The c file is then compiled into a .so file (a DLL under Windows). The following action generates a adder.so file

#For linux$   -shared-wl,-soname,adder-o adder.so--shared-wl,-install_name,adder.so-o adder.so--shared-wl,- Soname,adder-o Adder.dll-fpic ADD.C

Now call it in your Python code

 fromcTYPESImport*#load the Shared object fileAdder = Cdll ('./adder.so')#Find sum of integersRes_int = Adder.add_int (4,5)Print "Sum of 4 and 5 ="+Str (res_int)#Find sum of floatsA = C_float (5.5) b= C_float (4.1) Add_float=Adder.add_floatadd_float.restype=c_floatPrint "Sum of 5.5 and 4.1 =", str (Add_float (A, B))

The output is as follows

of 4 and 5 = 9Sum of 5.5 and 4.1 = 9.60000038147

In this example, the C file is self-explanatory, and it contains two functions, each of which implements the integer sum and the floating-point summation.

In a python file, first import the cTYPES module and then use the Cdll function to load the library file we created. This allows us to use the adder functions in the C class library through variables. When adder.add_int() called, a call to the C function is initiated internally add_int . The cTYPES interface allows us to use the default string and integer types in native Python when calling C functions.

For other types like Boolean and float, you must use the correct CType type. To pass a parameter to a adder.add_float() function, we first convert the decimal value in Python to the C_float type before it can be passed to the C function. This method is simple and clear, but it is very limited. For example, you cannot manipulate an object in C.

Python calls the C language

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.