Thursday 18 May 2023

Can't install pybind on macOS

I am learning pybind and follwing this tutorial https://pybind11.readthedocs.io/en/stable/installing.html

My actions

python3.10 -m venv venv
source venv/bin/activate
pip install pybind11

Then I am creating example.cpp

#include <pybind11/pybind11.h>

int add(int i, int j) {
    return i + j;
}

PYBIND11_MODULE(example, m) {
    m.doc() = "pybind11 example plugin"; // optional module docstring

    m.def("add", &add, "A function that adds two numbers");
}

And trying

g++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix)

I had an error:

Undefined symbols for architecture arm64: "_PyBaseObject_Type", referenced from: pybind11::detail::make_object_base_type(_typeobject*) in example-243936.o

Fixed the error with help of -undefined dynamic_lookup

g++ -O3 -Wall -shared -std=c++11  -undefined dynamic_lookup   -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix)

Then I received DLL library example.cpython-39-darwin.so I was trying to use the library

# python
Python 3.10.5 (v3.10.5:f377153967, Jun  6 2022, 12:36:10) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pybind11
>>> import example
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'example'
>>> 

How to fix the import error?

Details

MacOS version 12.2.1

pip list
Package    Version
---------- -------
pip        22.0.4
pybind11   2.10.4
setuptools 58.1.0

Python 3.10.5

g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.27.3)
Target: arm64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin


from Can't install pybind on macOS

No comments:

Post a Comment