blob: ac7062ce56c867d62a7982300a586460822eb930 [file] [log] [blame]
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001.. _basics:
2
3First steps
4###########
5
6This sections demonstrates the basic features of pybind11. Before getting
7started, make sure that development environment is set up to compile the
8included set of examples, which also double as test cases.
9
10
11Compiling the test cases
12========================
13
14Linux/MacOS
15-----------
16
17On Linux you'll need to install the **python-dev** or **python3-dev** packages as
18well as **cmake**. On Mac OS, the included python version works out of the box,
19but **cmake** must still be installed.
20
21After installing the prerequisites, run
22
23.. code-block:: bash
24
25 cmake .
26 make -j 4
27
28followed by
29
30.. code-block:: bash
31
32 make test
33
34Windows
35-------
36
37On Windows, use the `CMake GUI`_ to create a Visual Studio project. Note that
38only the 2015 release and newer versions are supported since pybind11 relies on
39various C++11 language features that break older versions of Visual Studio.
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020040After running CMake, open the created :file:`pybind11.sln` file and perform a
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020041release build, which will will produce a file named
42:file:`Release\\example.pyd`. Copy this file to the :file:`example` directory
43and run :file:`example\\run_test.py` using the targeted Python version.
44
45.. _`CMake GUI`: https://cmake.org/runningcmake
46
47.. Note::
48
49 When all tests fail, make sure that
50
51 1. The Python binary and the testcases are compiled for the same processor
52 type and bitness (i.e. either **i386** or **x86_64**)
53
54 2. The Python binary used to run :file:`example\\run_test.py` matches the
55 Python version specified in the CMake GUI. This is controlled via
56 the ``PYTHON_EXECUTABLE`` ``PYTHON_INCLUDE_DIR``, and
57 ``PYTHON_LIBRARY`` variables.
58
59.. seealso::
60
61 Advanced users who are already familiar with Boost.Python may want to skip
62 the tutorial and look at the test cases in the :file:`example` directory,
63 which exercise all features of pybind11.
64
65Creating bindings for a simple function
66=======================================
67
68Let's start by creating Python bindings for an extremely simple function, which
69adds two numbers and returns their result:
70
71.. code-block:: cpp
72
73 int add(int i, int j) {
74 return i + j;
75 }
76
77For simplicity [#f1]_, we'll put both this function and the binding code into
78a file named :file:`example.cpp` with the following contents:
79
80.. code-block:: cpp
81
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020082 #include <pybind11/pybind11.h>
Wenzel Jakob93296692015-10-13 23:21:54 +020083
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020084 int add(int i, int j) {
85 return i + j;
86 }
87
Wenzel Jakob10e62e12015-10-15 22:46:07 +020088 namespace py = pybind11;
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020089
Wenzel Jakobb1b71402015-10-18 16:48:30 +020090 PYBIND11_PLUGIN(example) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020091 py::module m("example", "pybind11 example plugin");
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020092
93 m.def("add", &add, "A function which adds two numbers");
94
95 return m.ptr();
96 }
97
Wenzel Jakobb1b71402015-10-18 16:48:30 +020098The :func:`PYBIND11_PLUGIN` macro creates a function that will be called when an
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020099``import`` statement is issued from within Python. The next line creates a
100module named ``example`` (with the supplied docstring). The method
101:func:`module::def` generates binding code that exposes the
102``add()`` function to Python. The last line returns the internal Python object
103associated with ``m`` to the Python interpreter.
104
105.. note::
106
107 Notice how little code was needed to expose our function to Python: all
108 details regarding the function's parameters and return value were
109 automatically inferred using template metaprogramming. This overall
110 approach and the used syntax are borrowed from Boost.Python, though the
111 underlying implementation is very different.
112
113pybind11 is a header-only-library, hence it is not necessary to link against
114any special libraries (other than Python itself). On Windows, use the CMake
115build file discussed in section :ref:`cmake`. On Linux and Mac OS, the above
116example can be compiled using the following command
117
118.. code-block:: bash
119
John Traversf7e43022015-12-12 22:58:23 +0100120 $ c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags --libs` example.cpp -o example.so
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200121
122In general, it is advisable to include several additional build parameters
123that can considerably reduce the size of the created binary. Refer to section
124:ref:`cmake` for a detailed example of a suitable cross-platform CMake-based
125build system.
126
127Assuming that the created file :file:`example.so` (:file:`example.pyd` on Windows)
128is located in the current directory, the following interactive Python session
129shows how to load and execute the example.
130
131.. code-block:: python
132
Wenzel Jakob93296692015-10-13 23:21:54 +0200133 $ python
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200134 Python 2.7.10 (default, Aug 22 2015, 20:33:39)
135 [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
136 Type "help", "copyright", "credits" or "license" for more information.
137 >>> import example
138 >>> example.add(1, 2)
139 3L
Wenzel Jakob93296692015-10-13 23:21:54 +0200140 >>>
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200141
142.. _keyword_args:
143
144Keyword arguments
145=================
146
147With a simple modification code, it is possible to inform Python about the
148names of the arguments ("i" and "j" in this case).
149
150.. code-block:: cpp
151
152 m.def("add", &add, "A function which adds two numbers",
153 py::arg("i"), py::arg("j"));
154
155:class:`arg` is one of several special tag classes which can be used to pass
156metadata into :func:`module::def`. With this modified binding code, we can now
157call the function using keyword arguments, which is a more readable alternative
158particularly for functions taking many parameters:
159
160.. code-block:: python
161
162 >>> import example
163 >>> example.add(i=1, j=2)
164 3L
165
166The keyword names also appear in the function signatures within the documentation.
167
168.. code-block:: python
169
170 >>> help(example)
171
172 ....
173
174 FUNCTIONS
175 add(...)
Wenzel Jakob6eb11da2016-01-17 22:36:36 +0100176 Signature : (i: int, j: int) -> int
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200177
178 A function which adds two numbers
179
180.. _default_args:
181
182Default arguments
183=================
184
185Suppose now that the function to be bound has default arguments, e.g.:
186
187.. code-block:: cpp
188
189 int add(int i = 1, int j = 2) {
190 return i + j;
191 }
192
193Unfortunately, pybind11 cannot automatically extract these parameters, since they
194are not part of the function's type information. However, they are simple to specify
195using an extension of :class:`arg`:
196
197.. code-block:: cpp
198
199 m.def("add", &add, "A function which adds two numbers",
200 py::arg("i") = 1, py::arg("j") = 2);
201
202The default values also appear within the documentation.
203
204.. code-block:: python
205
206 >>> help(example)
207
208 ....
209
210 FUNCTIONS
211 add(...)
Wenzel Jakob6eb11da2016-01-17 22:36:36 +0100212 Signature : (i: int = 1, j: int = 2) -> int
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200213
214 A function which adds two numbers
215
216.. _supported_types:
217
218Supported data types
219====================
220
221The following basic data types are supported out of the box (some may require
222an additional extension header to be included). To pass other data structures
Wenzel Jakob93296692015-10-13 23:21:54 +0200223as arguments and return values, refer to the section on binding :ref:`classes`.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200224
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200225+------------------------+--------------------------+-----------------------+
226| Data type | Description | Header file |
227+========================+==========================+=======================+
228| int8_t, uint8_t | 8-bit integers | pybind11/pybind11.h |
229+------------------------+--------------------------+-----------------------+
230| int16_t, uint16_t | 16-bit integers | pybind11/pybind11.h |
231+------------------------+--------------------------+-----------------------+
232| int32_t, uint32_t | 32-bit integers | pybind11/pybind11.h |
233+------------------------+--------------------------+-----------------------+
234| int64_t, uint64_t | 64-bit integers | pybind11/pybind11.h |
235+------------------------+--------------------------+-----------------------+
236| ssize_t, size_t | Platform-dependent size | pybind11/pybind11.h |
237+------------------------+--------------------------+-----------------------+
238| float, double | Floating point types | pybind11/pybind11.h |
239+------------------------+--------------------------+-----------------------+
240| bool | Two-state Boolean type | pybind11/pybind11.h |
241+------------------------+--------------------------+-----------------------+
242| char | Character literal | pybind11/pybind11.h |
243+------------------------+--------------------------+-----------------------+
244| const char * | UTF-8 string literal | pybind11/pybind11.h |
245+------------------------+--------------------------+-----------------------+
246| std::string | STL dynamic UTF-8 string | pybind11/pybind11.h |
247+------------------------+--------------------------+-----------------------+
248| std::pair<T1, T2> | Pair of two custom types | pybind11/pybind11.h |
249+------------------------+--------------------------+-----------------------+
250| std::tuple<....> | Arbitrary tuple of types | pybind11/pybind11.h |
251+------------------------+--------------------------+-----------------------+
252| std::complex<T> | Complex numbers | pybind11/complex.h |
253+------------------------+--------------------------+-----------------------+
254| std::vector<T> | STL dynamic array | pybind11/stl.h |
255+------------------------+--------------------------+-----------------------+
Wenzel Jakob6eb11da2016-01-17 22:36:36 +0100256| std::map<T1, T2> | STL ordered map | pybind11/stl.h |
257+------------------------+--------------------------+-----------------------+
258| std::set<T> | STL ordered set | pybind11/stl.h |
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200259+------------------------+--------------------------+-----------------------+
260| std::function<...> | STL polymorphic function | pybind11/functional.h |
261+------------------------+--------------------------+-----------------------+
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200262
263
264.. [#f1] In practice, implementation and binding code will generally be located
265 in separate files.
266