Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ctypes to write extensions in Python CumberCraft +

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use ctypes to write an extension, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Ctypes is a library of Python standard libraries. Designed to implement C data types such as calling DLL or shared libraries. It can be used to package these C libraries in a pure Python environment. This tutorial is carried out under MAC.

C++ Code # include

# ifdef _ _ cplusplus

# define XETR extern "C"

# else

# define XETR

# endif

# ifdef _ WIN32

# define LIB XETR _ _ declspec (dllexport)

# else

# define LIB XETR

# endif

LIB void CtypesDemo (const wchar_t * str,int x reint y) {

Printf ("Cpp% ls% d\ n", str,x+y)

}

Because the system is different, the way of calling is different.

_ WIN32: indicates win32 and win64

_ _ declspec (dllexport) is used in dynamic libraries in Windows to declare exported functions, classes, objects, etc., for external calls.

The function of extern "C" is to let C++ compiler treat the code declared by extern "C" as C language code, which can avoid the problem that C++ code can not be linked with symbols in C language library due to symbol modification.

Compile the C++ file into a so file named Gmail +-fPIC-shared-o ctypes ctypes.cpp

Windows users can use Visual studio 2015 to compile and generate dll files.

There are no dll files in mac and linux, usually so files.

Python code calls from ctypes import *

Lib=CDLL ("ctypes")

Lib.CtypesDemo ("official account: Python Learning Development", 4pr. 5)

There are about so many basic uses of ctype. In fact, when writing a Python extension library, if you want to eventually generate a Python module and import it for use, you have to follow Python API to write, and all function definitions follow certain rules.

After reading the above, do you have any further understanding of how to write an extension in Python C _ contract + using ctypes? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report