SWIG issue using cmake on Mac - can't import .dylib file

43 views Asked by At

I'm trying to build a SWIG package on Mac OS for the first time, after having already built it on my Windows box.

I'm using cmake, and it all compiles correctly. But when I try to import the package (called "bgstrat") in Python, it fails trying to import the generated _bgstrat.dylib file somehow. I checked that the Python and SWIG'd package are both 64-bit arm64 builds. I can even do

>>> import ctypes.util
>>> ctypes.util.find_library('_bgstrat')
'_bgstrat.dylib'
>>> ctypes.CDLL('_bgstrat.dylib')
<CDLL '_bgstrat.dylib', handle 8113d4d0 at 0x102ca7850>

So the problem isn't that _bgstrat.dylib isn't in the PYTHONPATH, and it's not a library compatibility problem.

Any ideas on what to look for next to track this down?

My CMakeFiles.txt file is:

cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(bgbot)

set(CMAKE_CXX_STANDARD 17)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# SWIG and Python

find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_PATH})

set(CMAKE_SWIG_FLAGS "")

# add SWIG module

set_source_files_properties(bgstrat.i PROPERTIES CPLUSPLUS ON)
swig_add_library(bgstrat TYPE SHARED LANGUAGE python SOURCES bgstrat.i board.cpp possboards.cpp)
swig_link_libraries(bgstrat ${PYTHON_LIBRARIES})

I expected that the import would work properly; but even though the .dylib file is in the path and can be loaded manually using ctypes.CDLL, the module itself won't load.

EDIT: solved it - I needed to tell SWIG to generate a .so file instead of a .dylib file, by adding this line to the end of my CMakeLists.txt file:

set_target_properties(bgstrat PROPERTIES SUFFIX ".so")

0

There are 0 answers