Skip to content

Commit 74037cf

Browse files
authored
FIX: Use dynamic_lookup for Python Library in macOS (microsoft#98)
1 parent be61e9a commit 74037cf

File tree

1 file changed

+5
-25
lines changed

1 file changed

+5
-25
lines changed

mssql_python/pybind/CMakeLists.txt

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -229,31 +229,11 @@ if(APPLE)
229229
# Add ODBC include directories on macOS
230230
target_include_directories(ddbc_bindings PRIVATE ${CMAKE_SOURCE_DIR}/../libs/odbc_include ${ODBC_INCLUDE_DIR})
231231

232-
# For macOS, we need to link against the Python framework
233-
if(PYTHON_LIBRARY)
234-
# If we found a specific library file, use it
235-
target_link_libraries(ddbc_bindings PRIVATE ${PYTHON_LIBRARY} dl)
236-
else()
237-
# Otherwise, try to link to the framework
238-
execute_process(
239-
COMMAND python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')"
240-
OUTPUT_VARIABLE PYTHON_VERSION_FULL
241-
OUTPUT_STRIP_TRAILING_WHITESPACE
242-
)
243-
244-
# Try to find the Python framework
245-
find_library(PYTHON_FRAMEWORK Python HINTS /Library/Frameworks/Python.framework/Versions/${PYTHON_VERSION_FULL}/lib)
246-
247-
if(PYTHON_FRAMEWORK)
248-
message(STATUS "Found Python framework: ${PYTHON_FRAMEWORK}")
249-
target_link_libraries(ddbc_bindings PRIVATE ${PYTHON_FRAMEWORK} dl)
250-
else()
251-
# As a last resort, try with -undefined dynamic_lookup flag
252-
message(STATUS "Python framework not found, using dynamic lookup")
253-
set_target_properties(ddbc_bindings PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
254-
target_link_libraries(ddbc_bindings PRIVATE dl)
255-
endif()
256-
endif()
232+
# For macOS, always use dynamic lookup for Python symbols
233+
# This ensures the extension works in any Python environment including virtual environments
234+
message(STATUS "Using dynamic lookup for Python symbols on macOS (best practice for Python extensions)")
235+
set_target_properties(ddbc_bindings PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
236+
target_link_libraries(ddbc_bindings PRIVATE dl)
257237

258238
# Set macOS-specific compile options
259239
target_compile_options(ddbc_bindings PRIVATE

0 commit comments

Comments
 (0)