Replace PYBIND11_PLUGIN with PYBIND11_MODULE
This commit also adds `doc()` to `object_api` as a shortcut for the
`attr("__doc__")` accessor.
The module macro changes from:
```c++
PYBIND11_PLUGIN(example) {
pybind11::module m("example", "pybind11 example plugin");
m.def("add", [](int a, int b) { return a + b; });
return m.ptr();
}
```
to:
```c++
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin";
m.def("add", [](int a, int b) { return a + b; });
}
```
Using the old macro results in a deprecation warning. The warning
actually points to the `pybind11_init` function (since attributes
don't bind to macros), but the message should be quite clear:
"PYBIND11_PLUGIN is deprecated, use PYBIND11_MODULE".
diff --git a/docs/faq.rst b/docs/faq.rst
index 34002b4..072b157 100644
--- a/docs/faq.rst
+++ b/docs/faq.rst
@@ -4,30 +4,24 @@
"ImportError: dynamic module does not define init function"
===========================================================
-1. Make sure that the name specified in ``pybind::module`` and
- ``PYBIND11_PLUGIN`` is consistent and identical to the filename of the
- extension library. The latter should not contain any extra prefixes (e.g.
- ``test.so`` instead of ``libtest.so``).
-
-2. If the above did not fix your issue, then you are likely using an
- incompatible version of Python (for instance, the extension library was
- compiled against Python 2, while the interpreter is running on top of some
- version of Python 3, or vice versa)
+You are likely using an incompatible version of Python (for instance, the
+extension library was compiled against Python 2, while the interpreter is
+running on top of some version of Python 3, or vice versa).
"Symbol not found: ``__Py_ZeroStruct`` / ``_PyInstanceMethod_Type``"
========================================================================
-See item 2 of the first answer.
+See the first answer.
"SystemError: dynamic module not initialized properly"
======================================================
-See item 2 of the first answer.
+See the first answer.
The Python interpreter immediately crashes when importing my module
===================================================================
-See item 2 of the first answer.
+See the first answer.
CMake doesn't detect the right Python version
=============================================
@@ -104,14 +98,10 @@
void init_ex2(py::module &);
/* ... */
- PYBIND11_PLUGIN(example) {
- py::module m("example", "pybind example plugin");
-
+ PYBIND11_MODULE(example, m) {
init_ex1(m);
init_ex2(m);
/* ... */
-
- return m.ptr();
}
:file:`ex1.cpp`: