Skip to content

Commit 88ae0c1

Browse files
committed
Use hidden visibility always; set via cmake property rather than compiler flag
This should resolve the issues with default visibility causing problems under debug compilations. Moreover using the cmake property makes it easier for a caller to override if absolutely needed for some reason.
1 parent 3879045 commit 88ae0c1

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

tools/pybind11Tools.cmake

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ function(pybind11_add_module target_name)
139139
set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
140140
set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
141141

142+
# -fvisibility=hidden is required to allow multiple modules compiled against
143+
# different pybind versions to work properly, and for some features (e.g.
144+
# py::module_local). We force it on everything inside the `pybind11`
145+
# namespace; also turning it on for a pybind module compilation here avoids
146+
# potential warnings or issues from having mixed hidden/non-hidden types.
147+
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
148+
142149
if(WIN32 OR CYGWIN)
143150
# Link against the Python shared library on Windows
144151
target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
@@ -175,9 +182,6 @@ function(pybind11_add_module target_name)
175182
_pybind11_add_lto_flags(${target_name} ${ARG_THIN_LTO})
176183

177184
if (NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug)
178-
# Set the default symbol visibility to hidden (very important to obtain small binaries)
179-
target_compile_options(${target_name} PRIVATE "-fvisibility=hidden")
180-
181185
# Strip unnecessary sections of the binary on Linux/Mac OS
182186
if(CMAKE_STRIP)
183187
if(APPLE)

0 commit comments

Comments
 (0)