In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to call c language code by python". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to call c language code by python" can help you solve the problem.
Development environment
Linux: python3.5.2 + ubuntu-gnome-16.04-desktop-amd64
Windows:cygwin + powershell + python3.6
Why does the glue language python call the c code?
C compared with python, python is not good at "massive computing" tasks. The advantage of python program is that it is easy to write and suitable for "IO-intensive jobs" (such as opening files, downloading pictures, running scripts). Python, as a well-known "glue language", the way to quickly implement "compute-intensive jobs" is to "take the c code and use it directly".
The ctypes module in Python is probably the simplest way for Python to call C methods. The ctypes module provides C-compatible data types and functions to load dll files, so there is no need to make any changes to the source file when calling, which establishes the simplicity of this method.
1. Linux
1. Prepare the C language program and save it as add.c
# include 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;}
two。 Compiled into a so library
Execute under Ubuntu: gcc-shared-Wl,-soname,adder-o adder.so-fPIC add.c
3. Prepare the python code and save it as python-c.py
Import ctypes # load the shared object fileadder = ctypes.cdll.LoadLibrary ('. / adder.so') # Find sum of integersres_int = adder.add_int (4) print ("4 + 5 =" + str (res_int)) # Find sum of floatsa = ctypes.c_float (5.5) b = ctypes.c_float (4.1) add_float = adder.add_floatadd_float.restype = ctypes.c_float print ("5.5 + 4.1 =" + str (add_float (a, b))
4. test
Execute: python3 python-c.py
The results are as follows:
4 + 5 = 9
5.5 + 4.1 = 9.600000381469727
5. Description
In the Python file, import the ctypes module first, and then use cdll.LoadLibrary
Function to load the library file we created. This allows us to use the functions in the C class library through the variable adder. When adder.add_int () is called, an internal call to the C function add_int is initiated. The ctypes interface allows us to use the default string and integer types in native Python when calling C functions.
For other types such as Boolean and floating point, you must use the correct ctype type. For example, when passing parameters to the adder.add_float () function, we have to convert the decimal value in Python to type c_float before passing it to the C function. This method is simple and clear, but it is very limited. For example, objects cannot be manipulated in C #.
II. Windows
1. Prepare the C language program and save it as add.c
Same as above
two。 Compiled into a dll library
Execute under cygwin: gcc-shared-Wl,-soname,adder-o adder.dll-fPIC add.c
3. Prepare the python code and save it as python-c.py
Import ctypes # load the shared object fileadder = ctypes.cdll.LoadLibrary ('.\ adder.dll') # Find sum of integersres_int = adder.add_int (4) print ("4 + 5 =" + str (res_int)) # Find sum of floatsa = ctypes.c_float (5.5) b = ctypes.c_float (4.1) add_float = adder.add_floatadd_float.restype = ctypes.c_float print ("5.5 + 4.1 =" + str (add_float (a, b)
4. test
Execute under powershell: .python-c.py
The following error occurs:
OSError: [WinError 126]
Reason: adder.dll itself depends on other libraries, which need to be copied to the current directory.
Execute under cygwin: ldd adder.dll
Tips depend on the following libraries
Copy these libraries to the current directory and execute: cp xxx.dll.
Execute: .python-c.py again under powershell
This is the end of the introduction to "how python calls the c language code". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.