Python calls the C language method [Based on the ctypes module], pythonctypes

Source: Internet
Author: User

Python calls the C language method [Based on the ctypes module], pythonctypes

This example describes how to call C language in Python. We will share this with you for your reference. The details are as follows:

The ctypes module in Python may be the simplest way for Python to call the C method. The ctypes module provides data types and functions compatible with the C language to load dll files. Therefore, you do not need to modify the source files during the call. This laid the simplicity of this method.

Example:

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

//sample C file to add 2 numbers - int and floats#include <stdio.h>int add_int(int, int);float add_float(float, float);int add_int(int num1, int num2){  return num1 + num2;}float add_float(float num1, float num2){  return num1 + num2;}

Next, compile the C file as the. so file (DLL in windows ). The following Operation generates the adder. so file

#For Linux$ gcc -shared -Wl,-soname,adder -o adder.so -fPIC add.c#For Mac$ gcc -shared -Wl,-install_name,adder.so -o adder.so -fPIC add.c#For windows$gcc -shared -Wl,-soname,adder -o adder.dll -fPIC add.c

Now you can call it in your Python code.

from ctypes import *#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))

Output:

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

In this example, the C file is self-explanatory. It contains two functions, namely integer summation and floating-point summation.

In the Python file, first import the ctypes module and then use the CDLL function to load the library file we created. In this way, we can use the variable adder to use functions in the C class library. Whenadder.add_int()When called, a call to C function add_int is initiated internally. The ctypes interface allows us to use the default string type and integer type in native Python when calling the C function.

For other types such as Boolean and floating-point, you must use the correct ctype. Ru Xiangadder.add_float()When passing parameters through a function, we must first convert the decimal value in Python to the c_float type before transmitting it to the C function. Although this method is simple and clear, it is very limited. For example, objects cannot be operated in C.

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.