I am currently using Pybind11 to create Python extensions for my C++ library, but now I have encountered a problem: I write setup.py and package the extension into a wheel package, but at runtime it will report an error saying that the corresponding shared library cannot be found :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libtest_linux64.so: cannot open shared object file: No such file or directory
Part of my code is as follows:
ext_modules = [
Pybind11Extension(
"test",
sources=[
"src/test.cpp"
],
libraries=["lib/libtest_linux64.so"],
library_dirs=["lib"]
include_dirs=[
"include"
],
runtime_library_dirs=[sysconfig.get_config_var("LIBDIR")],
language='c++'
),
]
setup(
name='myname',
version=version_code,
author='author',
author_email='test@test.com',
long_description='',
ext_modules=ext_modules,
data_files=["lib/libtest_linux64.so"],
cmdclass={'build_ext': build_ext},
zip_safe=False,
python_requires=">=3.6",
)
This wheel package runs fine after being installed on the machine where I packaged it, but if I change this package to another machine, the above error will be reported.
I think the problem lies in runtime_library_dirs=[sysconfig.get_config_var("LIBDIR")]
, because this causes the search path of the extension runtime to be fixed on the LIBDIR of the python environment of the packaging machine when packaging, so in another It does not work on machines with different environments. Is there any solution to solve this problem?
P.S. I also want to do similar packaging on MacOS. Is there anything I need to pay attention to?