Skip to content

Commit 8b9c6c5

Browse files
committed
Update docs wrt hidden visibility
1 parent 17c332d commit 8b9c6c5

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

docs/advanced/misc.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ Naturally, both methods will fail when there are cyclic dependencies.
137137

138138
Note that pybind11 code compiled with hidden-by-default symbol visibility (e.g.
139139
via the command line flag ``-fvisibility=hidden`` on GCC/Clang), which is
140-
strongly recommended for pybind11 modules, can interfere with the ability to
140+
required proper pybind11 functionality, can interfere with the ability to
141141
access types defined in another extension module. Working around this requires
142-
manually export types that are accessed by multiple extension modules; pybind11
143-
provides a macro to do just this:
142+
manually exporting types that are accessed by multiple extension modules;
143+
pybind11 provides a macro to do just this:
144144

145145
.. code-block:: cpp
146146

docs/compiling.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ to an independently constructed (through ``add_library``, not
184184

185185
These include Link Time Optimization (``-flto`` on GCC/Clang/ICPC, ``/GL``
186186
and ``/LTCG`` on Visual Studio) and .OBJ files with many sections on Visual
187-
Studio (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an
187+
Studio (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an
188188
explanation on why these are needed.
189189

190190
Embedding the Python interpreter

docs/faq.rst

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,33 @@ specifying a larger value, e.g. ``-ftemplate-depth=1024`` on GCC/Clang. The
151151
culprit is generally the generation of function signatures at compile time
152152
using C++14 template metaprogramming.
153153

154+
.. _`faq:hidden_visibility`:
155+
156+
"‘SomeClass’ declared with greater visibility than the type of its field ‘SomeClass::member’ [-Wattributes]"
157+
============================================================================================================
158+
159+
This error typically indicates that you are compiling without the required
160+
``-fvisibility`` flag. pybind11 code internally forces hidden visibility on
161+
all internal code, but if non-hidden (and thus *exported*) code attempts to
162+
include a pybind type (for example, ``py::object`` or ``py::list``) you can run
163+
into this warning.
164+
165+
To avoid it, make sure you are specifying ``-fvisibility=hidden`` when
166+
compiling pybind code.
167+
168+
As to why ``-fvisibility=hidden`` is necessary, because pybind modules could
169+
have been compiled under different versions of pybind itself, it is also
170+
important that the symbols defined in one module do not clash with the
171+
potentially-incompatible symbols defined in another. While Python extension
172+
modules are usually loaded with localized symbols (under POSIX systems
173+
typically using ``dlopen`` with the ``RTLD_LOCAL`` flag), this Python default
174+
can be changed, but even if it isn't it is not always enough to guarantee
175+
complete independence of the symbols involved when not using
176+
``-fvisibility=hidden``.
177+
178+
Additionally, ``-fvisiblity=hidden`` can deliver considerably binary size
179+
savings. (See the following section for more details).
180+
154181

155182
.. _`faq:symhidden`:
156183

@@ -192,11 +219,14 @@ world. So we'll generally only want to export symbols for those functions which
192219
are actually called from the outside.
193220

194221
This can be achieved by specifying the parameter ``-fvisibility=hidden`` to GCC
195-
and Clang, which sets the default symbol visibility to *hidden*. It's best to
196-
do this only for release builds, since the symbol names can be helpful in
197-
debugging sessions. On Visual Studio, symbols are already hidden by default, so
198-
nothing needs to be done there. Needless to say, this has a tremendous impact
199-
on the final binary size of the resulting extension library.
222+
and Clang, which sets the default symbol visibility to *hidden*, which has a
223+
tremendous impact on the final binary size of the resulting extension library.
224+
(On Visual Studio, symbols are already hidden by default, so nothing needs to
225+
be done there.)
226+
227+
In addition to decreasing binary size, ``-fvisibility=hidden`` also avoids
228+
potential serious issues when loading multiple modules and is required for
229+
proper pybind operation. See the previous FAQ entry for more details.
200230

201231
Another aspect that can require a fair bit of code are function signature
202232
descriptions. pybind11 automatically generates human-readable function

0 commit comments

Comments
 (0)