0

I am building a Python extension in C++ using pybind11 and scikit-build. I base on the example provided at https://github.com/pybind/scikit_build_example/blob/master/setup.py.

My CMakelists boils down to this:

pybind11_add_module(_mylib MODULE ${SOURCE_FILES})
install(TARGETS _mylib DESTINATION .)

setup.py:

setup(
    name="mylib",
    version="0.0",
    packages=['mylib'],
    cmake_install_dir="mylib",
)

And on the python side I have in mylib/__init__.py:

from _mylib import *

This all works great. I can install the package with pip and importing mylib successfully imports the library by proxy. This proxy is necessary because I do more in the library than just the C++ library.

Except there is one problem. The name of the built library includes the tools chain. For my system it looks like _mylib.cpython-38-x86_64-linux-gnu.so where I expect _mylib.so. __init__.py cannot find library unless either I rename it manually on the python side or I change the so name.

How can I resolve this issue?

Aart Stuurman
  • 3,188
  • 4
  • 26
  • 44
  • It may be possible to adjust the library name using the `OUTPUT_NAME`, `OUTPUT_NAME_`, `LIBRARY_NAME` or `LIBRARY_NAME_` target properties, depending on how the cmake module you're using actually sets the name of the output file. Note that setting this via `OUTPUT_NAME`(`_`?) target properties would be preferrable, but if the cmake module actually sets the `LIBRARY_NAME` versions, you need to overwrite those, since those take precedence over the output name versions. You should set `RUNTIME_OUTPUT_NAME` version to for this to work on windows... – fabian Feb 17 '22 at 18:07
  • That part of the name is _necessary_ to avoid ABI conflicts. – Alex Reinking Feb 17 '22 at 18:52

1 Answers1

0

Conclusion: as Alex said this part of the name is necessary. See https://www.python.org/dev/peps/pep-3149/. Python will automatically figure out it can use _mylib.cpython-38-x86_64-linux-gnu.so if you import _mylib.

Aart Stuurman
  • 3,188
  • 4
  • 26
  • 44