File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Filter options
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Original file line number Diff line number Diff line change
1
+ Interfacing with C/C++ Libraries
2
+ ================================
3
+
4
+
5
+ ctypes
6
+ ------
7
+
8
+ `ctypes <https://docs.python.org/3/library/ctypes.html >`_ is the de facto
9
+ library for interfacing with C/C++, and it provides not only full access to
10
+ the native C interface of most major operating systems (e.g., kernel32 on
11
+ Windows, or libc on *nix), but also provides support for loading and
12
+ interfacing with dynamic libraries, such as DLLs or shared objects at runtime.
13
+ It does bring along with it a whole host of types for interacting with system
14
+ APIs, and allows you to rather easily define your own complex types, such
15
+ as structs and unions, and allows you to modify things such as padding and
16
+ alignment, if needed. It can be a bit crufty to use, but in conjunction with
17
+ the `struct <https://docs.python.org/3.5/library/struct.html>`_ module, you
18
+ are essentially provided full control over how your data types get translated
19
+ into something something usable by a C(++).
20
+
21
+ Struct Equivalents
22
+ ~~~~~~~~~~~~~~~~~~
23
+
24
+ :file: `MyStruct.h `
25
+
26
+ .. code-block :: c
27
+ :linenos:
28
+
29
+ struct my_struct {
30
+ int a;
31
+ int b;
32
+ };
33
+
34
+ :file: `MyStruct.py `
35
+
36
+ .. code-block :: python
37
+ :linenos:
38
+
39
+ import ctypes
40
+ class my_struct (ctypes .Structure ):
41
+ _fields_ = [(" a" , c_int),
42
+ (" b" , c_int)]
You can’t perform that action at this time.
0 commit comments