Skip to content

Commit 44ca0b1

Browse files
committed
Added clib interface file; populated ctypes
1 parent cee124e commit 44ca0b1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/scenarios/clibs.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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)]

0 commit comments

Comments
 (0)